SQL INNER JOIN ON table1.column1 = table2.column2或ON ..?

时间:2018-11-30 18:55:51

标签: mysql join inner-join

如果要通过INNER JOIN选择数据库但2 ON且OR之间为ON的SQL脚本。

这是我的错误脚本

SELECT * 
FROM user
INNER JOIN friend ON friend.id1_friend = user.id_user 
                  OR ON friend.id2_friend = user.id_user
WHERE id_user != '$_SESSION[user]' 
  AND friend_status != '2'
ORDER BY id_user DESC

2 个答案:

答案 0 :(得分:1)

在一张桌子上使用一次。您刚放完或处于其他情况。

 SELECT*FROM user
INNER JOIN friend ON 
(friend.id1_friend=user.id_user OR friend.id2_friend=user.id_user)
WHERE id_user != $_SESSION[user]
AND friend_status != '2'
ORDER BY id_user DESC

答案 1 :(得分:1)

SELECT * -- this is not smart
  FROM user u
  JOIN friend f
    ON u.id_user IN(f.id1_friend,f.id2_friend)
 WHERE u.id_user != '$_SESSION[user]' -- this is not safe
   AND f.friend_status != 2
 ORDER 
    BY u.id_user DESC