这是我的表结构 -
TABLE : COURSE_LOG
-----------------------------------------------------
| ID | USERNAME | COURSE_ID | LDATE |
-----------------------------------------------------
| 1 | user1 | 22 | 2013-06-01 |
-----------------------------------------------------
| 2 | user1 | 54 | 2013-06-03 |
-----------------------------------------------------
| 3 | user1 | 22 | 2013-06-03 |
-----------------------------------------------------
| 4 | user2 | 71 | 2013-06-04 |
-----------------------------------------------------
我想清楚地选择所有user1
的COURSE_ID
数据(连同它的日期)。由于两个相同COURSE_ID
条目之间的日期会有所不同,因此我想选择具有较新日期的行。我希望得到这样的结果-
-----------------------------
| COURSE_ID | LDATE |
-----------------------------
| 54 | 2013-06-03 |
-----------------------------
| 22 | 2013-06-03 |
-----------------------------
| 71 | 2013-06-04 |
-----------------------------
我不想要这个 -
-----------------------------
| 22 | 2013-06-01 | // THIS SHOULD BE OMITTED FROM RESULT BECAUSE THERE
----------------------------- // IS ANOTHER RECENT ENTRY WITH THE SAME COURSE_ID
我正在使用这个查询 -
SELECT DISTINCT(COURSE_ID), LDATE FROM COURSE_LOG
WHERE USERNAME = 'user1'
AND LDATE = (
SELECT LDATE
FROM COURSE_LOG
WHERE USERNAME = 'user1'
ORDER BY LDATE DESC
LIMIT 1
)
但它只选择一排。我该如何纠正?