1

请高手,我有三个表,我需要从中选择数据LEFT JOIN;并且我需要按字段q(存在于 中table3)进行排序,但仅在 state = 1 的地方(也存在于 中table3)。

我试过这个但它不起作用:

SELECT * FROM table 1
         LEFT JOIN table2 ON x = y
         LEFT JOIN table3 ON z=w
         WHERE w = 1
         ORDER BY q IN ( SELECT q FROM table3 WHERE state = 1);

请问有什么建议吗?

4

3 回答 3

3

这是怎么回事:

SELECT * FROM table 1
         LEFT JOIN table2 ON x = y
         LEFT JOIN table3 ON z=w
         WHERE w = 1
         ORDER BY case when state=1 then 0 else 1 end,q 
于 2012-12-17T11:25:49.833 回答
0

试试这个:

SELECT *,
    (SELECT q FROM table3 WHERE state = 1) as sort_order
FROM table 1
    LEFT JOIN table2 ON x = y
    LEFT JOIN table3 ON z=w
WHERE w = 1
ORDER BY sort_order

因为我不知道期望的结果应该是什么,所以这可能是正确的查询:

SELECT *
FROM table1 as t1
    LEFT JOIN table2 as t2 ON t1.x = t2.y
    LEFT JOIN table3 as t3 ON t2.z = t3.w AND t3.state = 1
WHERE t3.w = 1
ORDER BY t3.q
于 2012-12-17T11:26:57.933 回答
0

尝试这样的事情:

SELECT * FROM table 1
         LEFT JOIN table2 ON x = y
         LEFT JOIN table3 ON z=w
         WHERE w = 1 AND table3.state = 1
         ORDER BY q
于 2012-12-17T11:28:29.570 回答