MySQL How to print the latest purchase product information in the group

Source: Internet
Author: User
How MySQL prints the latest purchased product information in the array
There is a book table (Stitle is the product name, an ID can purchase more than one book, such as the user ID 1 purchase a, B 2 products)

SQL Code
  
   Field  ID      stitle    time      1       A, B,     2011-01-01      2       c,d,     2011-01-02      3       A,       2011-01-03       4       b,e,     2011-01-04      5       C,       2011-01-05

Now ask how to print the latest first 5 purchased product names, the order of display should be:


C,b,e,a,d







------Solution--------------------
SQL Code
Select Left (Group_concat (Stitle), ten) from (select TRIM (', ' from Stitle) as Stitle from ' book ' ORDER by Time DESC LIMIT 5) A
------Solution--------------------
I think it would be nice to search in reverse order 5.

And then into the array, because the repetition of less than 5 items repeats the process
------Solution--------------------
In fact, he must have a product catalog why not use it?
SQL Code
DROP TABLE IF EXISTS ' T1 '; CREATE TABLE t1 (ID int primary key auto_increment,stitle varchar (), time date), insert into T1 values (null, ' A-B ', ' 2011-01  -01 '); insert into T1 values (null, ' c,d ', ' 2011-01-02 '); insert into T1 values (null, ' A ', ' 2011-01-03 '); INSERT into T1 values  (NULL, ' b,e ', ' 2011-01-04 '); insert into T1 values (null, ' C ', ' 2011-01-05 ');D rop TABLE IF EXISTS ' T2 ';  CREATE TABLE t2 (ID int primary KEY auto_increment,name varchar), insert into T2 values (null, ' a '); INSERT into T2 values  (NULL, ' B '); INSERT into T2 values (null, ' C '); INSERT into T2 values (null, ' d '); INSERT into T2 values (null, ' E '); INSERT into T2 values (null, ' F '); Select T1.id, T2.name as Stitle, t1.time from T1, T2 where Find_in_set (T2.name, t1.stitle) Order by T1.time desc LIMIT 5
------Solution--------------------
SQL Code
DROP TABLE IF EXISTS ' t '; CREATE TABLE t (ID int PRIMARY key auto_increment,stitle varchar (), time date); INSERT into t values (' ', ' A, B, ', str_to_date (' 2011-01-01 ', '%y-%m-%d ')); INSERT into t values (' ', ' c,d, ', ' str_to_date (' 2011-01-02 ', '%y-%m-%d ')); INSERT into t values (', ' A, ', Str_to_date (' 2011-01-03 ', '%y-%m-%d ')); INSERT into t values (' ', ' b,e, ', ' str_to_date (' 2011-01-04 ', '%y-%m-%d ')) and insert into t values (' ', ' C, ', Str_to_date (' 2011-01-05 ', '%y-%m-%d ')); select * FROM (select Id,substring (stitle,1,1) as newsitle,time,1 as Order1 from Tunion allselect id,substring (stitle,3,1) as newsitle,time,2 as Order1 from T) awhere a.newsitle! = "Orde R by Time desc,order1 ASC Limit 0,5;+----+----------+------------+--------+| ID | Newsitle | Time |  Order1 |+----+----------+------------+--------+| 5 | C |      2011-01-05 |  1 | | 4 | B |      2011-01-04 |  1 | | 4 | e |      2011-01-04 |  2 | | 3 | A |      2011-01-03 |  1 | | 2 | C |      2011-01-02 | 1 |+----+----------+------------+--------+  
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.