简体   繁体   English

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

[英]SQL INNER JOIN ON table1.column1 = table2.column2 OR ON ..?

What the SQL script if you want select database by INNER JOIN but with 2 ON and OR between ON. 如果要通过INNER JOIN选择数据库,但使用2 ON和ON之间的OR,则使用什么SQL脚本。

This is my wrong script 这是我的错误脚本

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

on is use one time with one table. 一张桌子只能使用一次。 you just put or with other condition. 您刚刚提出的条件或其他条件。

 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
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将table1.column1的值设置为table2.column2的平均值 - How to set value of table1.column1 to an average of table2.column2 从表1连接表2中选择id,table1.column1 + table2.column2作为总数。 如果table2.column2没有值怎么办? - select id, table1.column1 + table2.column2 as total from table1 join table2. what if table2.column2 has no value? 提取table1.column2的总和,其中table1.column1包含来自table2.column2的值 - Extract a sum of table1.column2 where table1.column1 contains value from table2.column2 mysql更新表设置column3,其中table1.column1像concat('%',table2.column2,'%') - mysql update table set column3 where table1.column1 like concat ('%',table2.column2,'%') 选择最后一个不同的记录,其中 table1.column1 = table2.column2 - Select last distinct records where table1.column1 = table2.column2 [table1.column1=table2.column1] 和 [table1.column1=1 AND table2.column1=1] 之间的区别 - Difference between [table1.column1=table2.column1] and [table1.column1=1 AND table2.column1=1] 使用内部联接更新表列 - Update table column with inner join SQL 中查询错误不明确的列名 4 内连接表 - Query error ambiguous column name 4 inner join table in SQL 内联多列与一个表的MySQL - INNER JOIN MULTIPLE COLUMN WITH ONE TABLE MYSQL mysql更新列内部联接另一个表 - mysql update column inner join another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM