SQL查询结合了select语句

时间:2016-10-21 08:25:21

标签: sql sqlite

我有SQL查询,如下:

SELECT *r1.val, r2.prevVal
FROM (Select statement) AS r1,
(Select statement) AS r2
WHERE 
r1.object_id = r2.object_id

它工作,问题是,当r2 select语句没有返回任何内容时,整个语句不返回任何内容,因为它无法执行r1.object_id = r2.object_id

如果select语句r1中有值,并且select语句r2中没有值,我怎样才能重现输出值的相同语句?所以我可以访问值r1.val

1 个答案:

答案 0 :(得分:2)

使用LEFT加入,如:

SELECT *r1.val, r2.prevVal
FROM (Select statement) AS r1
LEFT JOIN (Select statement) AS r2 ON r1.object_id = r2.object_id    ---------------------Formatted as code