Skip to content

Instantly share code, notes, and snippets.

@larste
Created September 18, 2012 18:12
Show Gist options
  • Save larste/3744758 to your computer and use it in GitHub Desktop.
Save larste/3744758 to your computer and use it in GitHub Desktop.
Join multiple tables with different columns
SELECT i.*,
n.title,
n.url,
'NULL' as description
FROM items i JOIN news n ON n.item_id = i.id
UNION ALL
SELECT i.*,
j.title,
j.url,
'NULL' as description
FROM items i JOIN jobs j ON j.item_id = i.id
UNION ALL
SELECT i.*,
'NULL' AS title,
'NULL' AS url, q.description
FROM items i JOIN questions q ON q.item_id = i.id
ORDER BY created_at DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment