I´m doing a system where I join several tables in a query.
In most circumstances the following kind of query does the trick:
SELECT * FROM A JOIN B ON A.a = B.a JOIN C ON C.a = A.a WHERE A.a = 123;
Attribute A.a is the PK and used as FK in B.a and C.a.
In some circumstances there are no corresponding B.a and C.a. In others just C.a is missing.
As it is now, if there are no corresponding keys in B or C I get an empty set. As expected.
It would be convenient with a query that produces a set with attributes only for A if only A exists and adds B and C attributes if A.a = B.a etc.
I guess this requires a DBMS specific solution and not general SQL but I´m not sure.
Any ideas how this should be done if possible?
You want this:
The
left outer joinkeeps all the records in the first table (the one on the “left”) and any matching tables on the right. You can reference columns in any of the tables. If there is no match, then the value is NULL.