这是我的SQL查询:
SELECT pts.pts_id,
pts.pts_name,
meds.*
FROM pts
LEFT JOIN meds
ON pts.pts_id = meds.pts_id_fk
WHERE pts.id_fk = $id
AND pts_current = 1
表
pts
(即表A)具有以下数据:pts_id time
1 Joe
2 Jack
3 Jill
表
meds
(即表B)具有以下数据:pts_id_fk time
1 7AM
1 8AM
1 9AM
2 7AM
2 10AM
3 11AM
我想
ORDER BY
基于time
的查询结果。有什么建议怎么做?
最佳答案
SELECT pts.pts_id,
pts.pts_name,
meds.*
FROM pts
LEFT JOIN meds
ON pts.pts_id = meds.pts_id_fk
WHERE pts.id_fk = $id
AND pts_current = 1
ORDER BY pts.pts_id, meds.time