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 |+----+----------+------------+--------+