例1:
现在有 医嘱主表ORDER_MAIN(ID,ORDER_ID,ORDER_NAME)? A表
? ? ? ? 和医嘱执行表ORDER_EXEC(ID,ORDER_ID,PLAN_TIME)? ? B表
两个表是用ORDER_ID关联起来的,A表一个ORDER_ID对应一条数据,B表一个ORDER_ID对应多个字段,要求根据ORDER_ID查A表所有数据和B表最新的PLAN_TIME,并根据PLAN_TIME排序。
SELECT A.* ,
MAX(B.PLAN_DATE)
FROM ORDER_EXEC B,
(SELECT A.ID A.ORDER_ID,
A.ORDER_NAME
FROM EMG_ORDER_MAIN A
WHERE A.ORDER_ID IN ( '10713082','10713083','10715833' )) A
WHERE M.ORDER_ID = T.ORDER_ID
GROUP BY A.ID,A.ORDER_ID,A.ORDER_NAME
ORDER BY MAX(B.PLAN_DATE)
?用了MAX函数,group by的时候要将其他字段都group by。