Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine records from differing tables using JOIN and UNION

Tags:

mysql

I need to create a query to combine the data from two tables, I think possibly a combination of JOIN and UNION.

In this example, I need to list all names where the status is active, just once, with their wine, soda, dinner, dessert and fruit preferences combined, ordered by name.

I'm not sure if JOIN alone will work, since a name isn't always in both tables, and UNION is tricky since they don't have the same columns. I've tried using null values to make a UNION work, but that's been giving me duplicate rows for each name instead of grouping them into one row per name.

DRINK
====================================
name         status   wine    soda
----------   ------   ------  ------
John Smith   active   red     cola
Mary Jones   active   white   lemonade
Tom Brown    old      red     fanta
Judy White   active   red     dr pepper
Sam Wing     old      red     cola

FOOD
=============================================
name         status   dinner  dessert  fruit
----------   ------   ------  ------   ------
John Smith   active   steak   muffin   apple
Mary Jones   active   fish    cake     kiwi
Walter Yu    active   pasta   cake     banana
Jim Adams    old      steak   candy    apple
Adam Sheers  active   pasta   candy    grapes

I need the query to generate a result like this -

RESULT
==================================================================  
name         status    wine     soda       dinner  dessert  fruit
----------   ------    ------   ------     ------  ------   ------
Adam Sheers  active    -        -          pasta   candy    grapes
John Smith   active    red      cola       steak   muffin   apple
Judy White   active    red      dr pepper  -       -        -
Mary Jones   active    white    lemonade   fish    cake     kiwi
Walter Yu    active    -        -          pasta   cake     banana

A huge thanks for any help with this!

like image 422
itsneal Avatar asked Feb 17 '26 08:02

itsneal


1 Answers

This should work:

SELECT names.name, drink.wine, drink.soda, food.dinner, food.dessert, food.fruit
FROM
    (SELECT name FROM food WHERE status = 'active'
    UNION
    SELECT name FROM drink WHERE status = 'active') names
LEFT JOIN drink ON drink.name = names.name
LEFT JOIN food ON food.name = names.name

Result

|        NAME |   WINE |      SODA | DINNER | DESSERT |  FRUIT |
----------------------------------------------------------------
|  John Smith |    red |      cola |  steak |  muffin |  apple |
|  Mary Jones |  white |  lemonade |   fish |    cake |   kiwi |
|   Walter Yu | (null) |    (null) |  pasta |    cake | banana |
| Adam Sheers | (null) |    (null) |  pasta |   candy | grapes |
|  Judy White |    red | dr pepper | (null) |  (null) | (null) |

See it in action

like image 58
Kermit Avatar answered Feb 20 '26 06:02

Kermit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!