开发者

MySQL - If Table A is LEFT JOINed to Table B, how do I ORDER BY a field in Table B?

Here is my SQL query:

    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_cur开发者_开发知识库rent = 1

Table pts (ie Table A) has the following data:

pts_id   time
1        Joe
2        Jack
3        Jill

Table meds (ie Table B) has the following data:

pts_id_fk   time
1           7AM
1           8AM
1           9AM
2           7AM
2          10AM
3          11AM

I would like to ORDER BY the result of this query based on the time.

Any suggestions how to do this?


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


Uhm what about ORDER BY meds.time

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜