(A加入B)加入(C加入D)

时间:2013-10-15 05:43:45

标签: mysql sql

我想制作JOIN的具体顺序

SELECT *
FROM (lives_in as t1 NATURAL JOIN preferences p1) l1
JOIN (lives_in t2 NATURAL JOIN preferences p2) l2
ON l1.dormid = l2.dormid

返回错误。

任何人都可以提供帮助?非常感谢!

3 个答案:

答案 0 :(得分:2)

您的别名查询缺少SELECT子句,请尝试以下操作:

SELECT *
FROM (
    select * -- added this
    FROM lives_in as t1
    NATURAL JOIN preferences p1) l1
JOIN (
    select * -- added this
    FROM lives_in t2
    NATURAL JOIN preferences p2) l2
ON l1.dormid = l2.dormid

答案 1 :(得分:1)

连接顺序与结果无关。你可能想要这样的东西:

SELECT *
FROM lives_in t1 
NATURAL JOIN preferences p1 ON p1.some_id = t1.id
NATURAL JOIN preferences p2 ON p2.some_id = t1.id

此外,大多数人称之为INNER JOIN,而非天然加入,顺便说一句。

答案 2 :(得分:1)

希望它会有所帮助。

SELECT *
FROM (
select * from lives_in as t1 NATURAL JOIN preferences p1
) l1
JOIN (
select * from lives_in t2 NATURAL JOIN preferences p2
) l2
ON l1.dormid = l2.dormid