0

Table :

enter image description here

I am new to query writing. Now I am stuck on retrieving 2 rows from above table. Data will be date sorted in descending order for only 2 different topic_id. There won't be a third different topic_id.

So I want to retrieve two rows only that will have different topic_id, one data for each topic_id having most recent date.

The result would be

enter image description here

try sql fiddle

4

2 回答 2

3

http://sqlfiddle.com/#!2/f37963/9

SELECT t1.* FROM temp t1
      JOIN (SELECT question_id, MAX(`date`) as `date` FROM temp GROUP BY topic_id) t2
        ON t1.question_id= t2.question_id AND t1.`date`= t2.`date`;

逻辑是在每个组(子查询)中查找最新日期,然后再次将其与表连接以检索其他详细信息。

于 2013-04-18T06:42:00.307 回答
1

用这个

 $qry="SELECT * FROM table_name GROUP BY TOPIC_ID ORDER BY DATE desc";
于 2013-04-18T06:33:13.173 回答