【发布时间】:2015-01-22 20:04:07
【问题描述】:
我试图对这些表进行困难查询:
服务:
id_service description cost
1 service 1 20
2 service 2 30
3 service 3 25
历史:
id_history status id_service
1 0 1
2 1 1
3 2 1
4 3 1
5 0 2
6 4 1
7 1 2
8 2 2
9 0 3
10 1 3
11 3 2
我需要知道哪些服务不完整(状态!= 4)并按最近的更新分组。也许,像这样:
id_history status id_service description
11 3 2 service 2
8 2 2 service 2
7 1 2 service 2
5 0 2 service 2
10 1 3 service 3
9 0 3 service 3
这是我当前的查询:
SELECT a.*, b.descripcion
FROM history a
JOIN service b ON a.id_service = b.id_service
WHERE a.status != 4
GROUP BY a.id_service
ORDER BY a.id_history DESC
每个id_service 只返回一行:
ID_HISTORY STATUS ID_SERVICE DESCRIPTION
9 0 3 service 3
5 0 2 service 2
1 0 1 service 1
【问题讨论】:
-
我想你想
ORDER BY a.id_service, a.id_history DESC摆脱GROUP BY