我有一张桌子(table1):
ID INSERTDATE ISSUE SOURCE
1 2011-12-01 A EMAIL
2 2011-12-02 B POST
3 2011-12-03 C MEETING
4 2011-12-04 B INTERNET
我想为每个问题获取最新消息......
SELECT * FROM table1 GROUP BY SOURCE ORDER BY ISSUE ASC
结果
ID INSERTDATE ISSUE SOURCE
1 2011-12-01 A EMAIL
2 2011-12-02 B POST
3 2011-12-03 C MEETING
但是我怎样才能得到以下内容,以便问题B有最新的来源?
ID INSERTDATE ISSUE SOURCE
1 2011-12-01 A EMAIL
4 2011-12-04 B INTERNET
3 2011-12-03 C MEETING
答案 0 :(得分:1)
您需要聚合和自我加入。
这是标准SQL,不依赖于扩展的MySQL组(are rubbish)
SELECT
t.*
FROM
( -- get most recent ID per issue
SELECT MAX(ID) AS MaxID, ISSUE
FROM table1
GROUP BY ISSUE
) t2
JOIN -- join back to get the whole row
table1 t ON t2.MaxID = t.ID
评论后编辑。假设每天只更改一次
SELECT
t.*
FROM
( -- get most recent INSERTDATE per issue
SELECT MAX(INSERTDATE) AS MaxINSERTDATE, ISSUE
FROM table1
GROUP BY ISSUE
) t2
JOIN -- join back to get the whole row
table1 t ON t2.MaxINSERTDATE = t.ID
答案 1 :(得分:-1)
我认为你应该通过INSERTDATE订购(也许,问题)
SELECT * FROM table1 GROUP BY SOURCE ORDER BY INSERTDATE DESC