Mysql通过其他表调用结果顺序

时间:2012-08-16 09:43:58

标签: mysql

请允许我再次作为一个愚蠢的问题。 我很难从两个表中调用查询mysql。

post_table

    PostID | UID_frm

    10     | 1
    10     | 2
    10     | 3
    10     | 4
    10     | 5

USER_TABLE

    Name   | FID

    tom    | 2
    tom    | 3
    henry  | 4

我希望下面有这个结果。

UID_frm
2
3
1
4
5

这是来自UID_frm列的结果,但是来自user_table.FID的优先顺序。 请指教我想这样打电话:(但不起作用)

select UID_frm 
from post_table 
where PostID='10'  
order by (select FID 
            from user_table 
           where Name='tom')

2 个答案:

答案 0 :(得分:1)

我认为这就是你要找的东西:

SELECT UID_frm
FROM post_table a
     LEFT JOIN user_table b
        ON a.UID_frm = b.FID
WHERE PostID = '10'
ORDER BY (IF(b.Name = 'tom', b.FID, NULL)) ASC, a.UID_frm ASC

答案 1 :(得分:1)

试试这个

select PT.UID_frm 
    from post_table as PT,user_table as UT
    where UT.FID=PT.UID_frm and PT.PostID='10'
    order by UT.name ASC/DESC