将OR添加到内部连接语句

时间:2012-07-23 10:13:13

标签: php mysql

[TABLE1]

 +----------+-------------------+
 | USERNAME | POST              |
 +----------+-------------------+
 | Bob      | 'Hi There'        |
 | Jack     | 'Hello'           |
 | Bob      | 'Today is Monday' |
 +----------+-------------------+

 [TABLE2]

 +----------+-----------+
 | USERNAME | FOLLOWING |
 +----------+-----------+
 | Mike     | Jack      |
 | Jack     | Bob       |
 | Bob      | Jack      |
 | Jack     | Mike      |
 +----------+-----------+

我正在使用此连接语句:

 SELECT t1.* FROM TABLE1 t1 INNER JOIN table2 t2 
 ON t1.username = t2.following AND t2.username = 'jack';

这有效,但除了返回这些结果外,我还想添加“OR t1.USERNAME ='jack';

然后另一个OR ..有一种方法可以进行连接,但是还有一个OR语句

4 个答案:

答案 0 :(得分:1)

将这些条件放在where子句

SELECT t1.* FROM TABLE1 t1
INNER JOIN table2 t2  ON t1.username = t2.following
where t2.username = 'jack'
OR t1.USERNAME = 'jack'

答案 1 :(得分:1)

您应在括号内指定多个OR条件:

SELECT t1.* 
FROM TABLE1 t1 
     INNER JOIN table2 t2  
         ON t1.username = t2.following 
            AND (t2.username = 'jack' OR t2.username = 'John' OR t2.username = 'Tom');

答案 2 :(得分:0)

是的,你可以这样做

 SELECT t1.* FROM TABLE1 t1 INNER JOIN table2 t2 ont1.colName1=t2.Colname2 where colname ='value' or colname='vaue1' or colname='value';

答案 3 :(得分:0)

简化方法

SELECT t1.* FROM TABLE1 t1 
INNER JOIN table2 t2  ON t1.username = t2.following 
where 'jack' in (t1.USERNAME,t2.username)