简体   繁体   中英

Combine records from differing tables using JOIN and UNION

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!

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

SELECT name, status, MAX(wine) wine, MAX(soda) soda, MAX(dinner) dinner, MAX(dessert) dessert, MAX(fruit) fruit
FROM
  (
    SELECT name, status, wine, soda, NULL dinner, NULL dessert, NULL  fruit
    FROM DRINK WHERE status = 'active'
    UNION ALL
    SELECT name, status, NULL wine, NULL soda, dinner, dessert, fruit
    FROM FOOD WHERE status = 'active'
  ) R
  GROUP BY name, status
select * 
from food f 
left join drink d on f.name = d.name 
where d.status = 'active' and f.status = 'active'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM