我在php文件中有一个MySQL查询。我想通过使用'ORDER by value ASC'从其中一个表中的列升序值来对结果进行排序。但是,当前查询生成的结果不是按此值排序,而是表中的另一个值。我认为这是因为我的语法错误,但我无法弄清楚如何。我一直试图解决这个问题几个星期但没有快乐,有人可以帮忙吗?
非常感谢您的回复。
查询:
$Find_Query3 = mysql_query("SELECT
patients.*,
addobs.*,
DATE_FORMAT(addobs.datetime, '%d/%m/%Y %H:%i:%s') as dti1,
(SELECT DATE_FORMAT(MIN(addobs.datetime), '%m/%d/%Y %H:%i:%s') FROM addobs WHERE addobs.mrn = patients.mrn AND addobs.datetime >= lastOkPar.lastDatetime AND addobs.par >= $par AND addobs.hidden != 'yes') as dti2
FROM addobs
INNER JOIN patients ON addobs.MRN = patients.MRN
LEFT JOIN (
SELECT
addobs.MRN
, MAX(addobs.datetime) as lastDatetime
FROM addobs
WHERE
addobs.par < $par
AND addobs.hidden != 'yes'
GROUP BY addobs.MRN
) lastOkPar ON lastOkPar.MRN = addobs.MRN
WHERE
addobs.datetime = (SELECT MAX(OLAST.datetime) FROM addobs AS OLAST WHERE OLAST.MRN = patients.MRN)
AND addobs.par >= $par
AND NOT addobs.hidden = 'yes'
AND COALESCE(patients.ward,'') != 'dc'
ORDER by addobs.par ASC, dti2 ASC
PS目前的结果是按出生日期排序的,这是“患者”的一栏。我不知道它为什么选择按此订购。我确定有逻辑,但我已经解决了!
谢谢,
马特
答案 0 :(得分:0)
试试这个
ORDER by addobs.par ,
(SELECT DATE_FORMAT(MIN(addobs.datetime), '%m/%d/%Y %H:%i:%s') FROM addobs WHERE addobs.mrn = patients.mrn AND addobs.datetime >= lastOkPar.lastDatetime AND addobs.par >= $par AND addobs.hidden != 'yes')