Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- A.id A.name B.id B.name C.id C.name
- -----------------------------------
- 1 Thing 2 Bee NULL NULL
- 1 Thing NULL NULL 1 Cow
- (
- SELECT
- A.id AS a_id,
- A.name AS a_name,
- B.id AS b_id,
- B.name AS b_name,
- /* Substitute NULLs... for the other table C, for *all columns* */
- NULL AS c_id,
- NULL AS c_name,
- NULL AS other_c
- /* etc, other cols from C */
- FROM A
- LEFT JOIN B ON A.id = B.a_id
- /* UNION, rather than UNION ALL in case both tables have NULLs */
- ) UNION (
- SELECT
- A.id AS a_id,
- A.name AS a_name,
- /* This time substitute NULLs for B, again *all columns* */
- NULL AS b_id,
- NULL AS b_name,
- C.id AS c_id,
- C.name AS c_name
- C.other_c
- FROM A
- LEFT JOIN C ON A.id = C.a_id
- )
- ORDER BY
- a_id,
- /* sort the non-null B ahead... */
- CASE WHEN b_id IS NOT NULL THEN 0 ELSE 1 END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement