两个内连接查询没有运行?

时间:2012-12-26 07:35:34

标签: mysql inner-join

我想在两张桌子中使用内心。
我的表格为usersrelationsusers(再次)。我想relationsusers sender_id加入reveiver_id并再次加入select * from (relations inner join users on relations.sender_id = users.userid) inner join users on relations.reciever_id = users.userid
我的示例代码是:

{{1}}

但此代码未运行。为什么呢?

2 个答案:

答案 0 :(得分:3)

如果要使用子查询,则可以使用此查询 -

SELECT * FROM 
  (SELECT * FROM `relations`
   INNER JOIN `users`
     ON `relations`.`sender_id` = `users`.`userid`) t
INNER JOIN `users`
  ON t.`reciever_id` = `users`.`userid`

但它与 -

相同
SELECT * FROM `relations`
INNER JOIN `users`
  ON `relations`.`sender_id` = `users`.`userid`
INNER JOIN `users`
  ON `relations`.`reciever_id` = `users`.`userid`

答案 1 :(得分:1)

SELECT * 
FROM `relations` AS r INNER JOIN 
`users` AS u1 on r.`sender_id` = u1.`userid` INNER JOIN 
`users` AS u2 on r.`reciever_id` = u2.`userid`