MySQL - 如果表A左表连接到表B,我如何对表B中的字段进行ORDER BY?

时间:2011-07-18 03:07:49

标签: mysql sql sql-order-by left-join

这是我的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 {{1}}此查询的结果。

有任何建议怎么做?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

嗯,ORDER BY meds.time

怎么样?