2

我在两个表之间使用 JOIN 时遇到问题,这是我的表:

病人桌

........................................
uid | no_mr     | name
........................................
1   | 1101      | Eko
2   | 1102      | John Doe

访问表

..............................................
uid | vcode         | patient_id
..............................................
1   | V-20130725143528  | 1
2   | V-20130726132308  | 2
3   | V-20130726142907  | 2
4   | V-20130726144436  | 1 

如何根据就诊表显示患者数据,并按visit.uid 降序排列。

这是我的查询,

SELECT patient . * , visit.uid
FROM patient
LEFT JOIN visit ON patient.uid = visit.patient_id
WHERE patient.uid >1
GROUP BY patient.no_mr
ORDER BY visit.uid DESC 
LIMIT 0 , 10

当我运行该查询时,我有这个值。

.......................................
uid | no_mr     | name
.......................................
2   | 1102      | John Doe
1   | 1101      | Eko

我想要这样,显示有新就诊数据的患者数据。

.......................................
uid | no_mr     | name
.......................................
1   | 1101      | Eko
2   | 1102      | John Doe

请帮忙,谢谢。。

4

1 回答 1

5

试试这个

SELECT q.* FROM (
SELECT patient . * , visit.uid AS visit_uid
FROM patient
LEFT JOIN visit ON patient.uid = visit.patient_id
ORDER BY visit_uid DESC 

) q GROUP BY q.uid ORDER BY q.visit_uid DESC 

LIMIT 0 , 10

小提琴

于 2013-07-26T19:42:57.683 回答