DB: PostgreSQL
Suppose I have
table A(integer a_id, timestamp x, timestamp y)
table B(int b_id, timestamp n, timestamp m)
m can be null
I want to fetch the data with this structure
SELECT a_id, j.b_id, k.b_id
FROM A, B AS j, B AS k
WHERE (x BETWEEN j.n AND j.m) AND (y BETWEEN k.n AND k.m.)
When (x BETWEEN j.n AND j.m) AND (y BETWEEN k.n AND k.m.)‘s matches aren’t not found
I still want the query to fetch the data with null j.b_id or null k.b_id
How do I do that?
My interpretation of the cryptic question …
If no matching row is found in
jork,NULLis delivered forj.b_idork.b_idrespectively.The query suffers from the principal design flaw that multiple matches in
bwill multiply the number of rows returned in aCROSS JOINfashion.