我有一张桌子,名为' person_data': -
id req_type reqp_id reqt_id dev_id
1 track 11 12 33
2 stop_track 11 12 33
3 track 12 13 44
4 stop_track 12 13 44
5 track 12 13 45
6 track 13 14 55
现在条件是我想要的: -
我尝试的是: -
select max(id),min(req_type),reqp_id, reqt_id, dev_id
from person_data group by reqp_id, reqt_id, dev_id;
并且输出与我想要的完全相同: -
id req_type reqp_id reqt_id dev_id
2 stop_track 11 12 33
4 stop_track 12 13 44
5 track 12 13 45
6 track 13 14 55
如果我使用: -
select max(id),max(req_type),reqp_id, reqt_id, dev_id
from person_data group by reqp_id, reqt_id, dev_id;
然后输出只改变了" req_type"列为: -
req_type
track
track
track
track
但是输出清楚地描述了max或min函数使用字母顺序对值进行排序,如果有人更改了" req_type"的值,我不想要这样做。列然后输出将被更改。 提前谢谢,
答案 0 :(得分:2)
试试这个
SELECT T.* FROM person_data T JOIN
(
SELECT Max(id) as maxid,reqp_id, reqt_id, dev_id
FROM person_data
GROUP BY reqp_id, reqt_id, dev_id
) S
ON T.id = S.maxid
<强> Fiddle Demo 强>
输出将是:
id req_type reqp_id reqt_id dev_id
2 stop_track 11 12 33
4 stop_track 12 13 44
5 track 12 13 45
6 track 13 14 55