我的表:
id | request | subject | date
1 | 5 | 1 | 576677
2 | 2 | 3 | 576698
3 | 5 | 1 | 576999
4 | 2 | 3 | 586999
需要通过两列(请求,主题)选择唯一记录,并显示最后插入的ID。
我的查询:
SELECT *,MAX(id)
FROM `tbl`
GROUP BY CONCAT(`request_id`, `subject_id`)
HAVING (COUNT(`request_id`)>1 order by MAX(id) desc
但结果:
id | request | subject | date
2 | 2 | 3 | 576698
1 | 5 | 1 | 576677
如何获取ID为3和4的记录?
答案 0 :(得分:1)
试试这个:
SELECT MIN(id) id, request, subject, MAX(`date`) `date`
FROM `tbl`
GROUP BY request, subject;
在SQL Fiddle上看到它。
答案 1 :(得分:1)
你可以试试这个。
SELECT T.*
FROM T
INNER JOIN
(
SELECT MAX(`ID`) as ID,`request`,`subject`
FROM T
GROUP BY `request`,`subject`
HAVING COUNT(`ID`) > 1
)AS T1 ON T.ID = T1.ID
答案 2 :(得分:0)
谢谢大家。我的结果是
SELECT MAX(id), id, request, subject, date
FROM `tbl`
GROUP BY request, subject having count(request)>1 order by MAX(id) desc