然后在内连接条件下

时间:2012-11-05 11:24:37

标签: mysql

SELECT col1,col2 FROM t1 INNER JOIN t2 ON t1.col1 = t2.id where t2.col2 ='1'
SELECT col1,col2 FROM t1 INNER JOIN t2 ON t1.col2 = t2.id where t2.col1 ='1'

我可以在单个查询中合并两个查询吗? 我尝试过当时使用!

3 个答案:

答案 0 :(得分:1)

UNION

怎么样?
SELECT col1,col2 FROM t1 INNER JOIN t2 ON t1.col1 = t2.id where t2.col2 ='1'
UNION 
SELECT col1,col2 FROM t1 INNER JOIN t2 ON t1.col2 = t2.id where t2.col1 ='1'

答案 1 :(得分:0)

SELECT col1,col2 FROM t1, t2 
WHERE 
(t1.col1 = t2.id AND t2.col2 = '1')
OR
(t1.col2 = t2.id AND t2.col1 = '1')

是另一种方式。但是UNION也应该起作用。

答案 2 :(得分:0)

这听起来像你想要的

SELECT col1, col2
FROM (
 SELECT col1,col2 FROM t1 INNER JOIN t2 ON t1.col1 = t2.id where t2.col2 ='1'
 UNION
 SELECT col1,col2 FROM t1 INNER JOIN t2 ON t1.col2 = t2.id where t2.col1 ='1'
     )
ORDER BY whatever