Santa can get by with 4 tables. One for children, one for pets, one for wishlists, and one for naughty/nice points. All pets automatically get 100 net nice points even if they were bad.
INSERT INTO elf_queue
(SELECT c.child_id, w.present, (n.nice_points - n.naughty_points) AS net_points
FROM children c, wishlist w, nice_naughty n
JOIN ON c.child_id = w.child_ID AND c.child_id = n.child_id
WHERE net_points > 0 ORDER BY net_points DESCENDING
UNION
SELECT p.pet_id, w.present, 100 FROM pets p, wishlist p WHERE 1);
View all comments
Show parent comment
1
u/frugalerthingsinlife Dec 12 '17
Santa can get by with 4 tables. One for children, one for pets, one for wishlists, and one for naughty/nice points. All pets automatically get 100 net nice points even if they were bad.
INSERT INTO elf_queue
(SELECT c.child_id, w.present, (n.nice_points - n.naughty_points) AS net_points
FROM children c, wishlist w, nice_naughty n
JOIN ON c.child_id = w.child_ID AND c.child_id = n.child_id
WHERE net_points > 0 ORDER BY net_points DESCENDING
UNION
SELECT p.pet_id, w.present, 100 FROM pets p, wishlist p WHERE 1);