我试图将这两个SQL查询结合起来。
SELECT * FROM `history` WHERE `ID` = 56
SELECT * FROM `live` WHERE `ID` = 56 AND `ACTIVE` = 0 AND t1.`Shown` = 'complete'
我尝试过这样的事情:
SELECT t1.*,t2.*
FROM `history` t1
JOIN `live` t2
ON t1.`ID` = 56 AND t1.`ACTIVE` = 0 AND t1.`Shown` = 'complete'
WHERE t2.`ID` = 56
但上述查询会导致从live
中选择所有内容并忘记history
。
预期结果:我希望从history
和live
中选择所有列ID is equal to 56
。但有些情况会导致ID's
中的history
不存在live
- 在这种情况下,我想从history
中选择所有ID
ID
1}}是平等的。
两个表都有不同的列。他们唯一的共同点是Using UNION with specific columns from both tables.
。
解决方案:
Downloading Coverity Scan Analysis Tool...
ERROR: The certificate of ‘scan.coverity.com’ is not trusted.
ERROR: The certificate of ‘scan.coverity.com’ hasn't got a known issuer.
答案 0 :(得分:0)
快速回答是:
SELECT t1.*,t2.*
FROM `live` t1
JOIN `history` t2
ON t1.id = t2.id
WHERE t2.`ID` = 56 AND t1.`ACTIVE` = 0 AND t1.`Shown` = 'complete'
但我想你应该用更多细节描述你的问题......
答案 1 :(得分:0)
假设两个表都有相同的列,听起来好像你想在这里使用UNION。
SELECT Col1, Col2, ..., ColN
FROM history
WHERE ID = 56
UNION
SELECT Col1, Col2, ..., ColN
FROM live
WHERE ID = 56
AND ACTIVE = 0
AND Shown = 'complete'
答案 2 :(得分:0)
如果history
和live
表具有相同的数字(并且最好是类型)列,那么UNION
查询可能就是您想要的:
SELECT * FROM `history` WHERE `ID` = 56
UNION ALL
SELECT * FROM `live` WHERE `ID` = 56 AND `ACTIVE` = 0 AND t1.`Shown` = 'complete'
如果列数不相同,则可以从两个表中选择相同数量的列以使查询起作用。
答案 3 :(得分:0)
如果history
和live
具有相同数量(并且最好是类型)列,那么这可以帮助您
SELECT * FROM
(
SELECT * FROM `history` WHERE `ID` = 56
UNION ALL
SELECT * FROM `live` WHERE `ID` = 56 AND `ACTIVE` = 0 AND t1.`Shown` = 'complete'
) as result
或强>
SELECT * FROM
history h JOIN live l ON
h.id = l.id
WHERE h.id = 56 AND l.id= 56 AND l.`ACTIVE` = 0 AND l.`Shown` = 'complete'