Evgeniy Kleban
Evgeniy Kleban

Reputation: 6940

SQL query combine select statements

I have SQL query like follow:

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

It work, problem is, when r2 select statement return nothing, therefore, whole statement return nothing, because it can't execute r1.object_id = r2.object_id.

How could I reproduce the same statement, which will output values, if there are values in select statement r1, and no values in select statement r2? So I can access value r1.val.

Upvotes: 1

Views: 71

Answers (1)

Chriz
Chriz

Reputation: 587

Use a LEFT Join like:

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

Upvotes: 2

Related Questions