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

我可以在单个查询中合并两个查询吗?我尝试使用WHEN THEN!

4

3 回答 3

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'
于 2012-11-05T11:26:26.273 回答
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
于 2012-11-05T11:59:09.210 回答
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也应该真的有效。

于 2012-11-05T11:38:27.180 回答