简体   繁体   English

使用JOIN和UNION组合来自不同表的记录

[英]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. 我需要创建一个查询来组合来自两个表的数据,我想可能是JOIN和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. 我不确定JOIN是否会起作用,因为名称并不总是在两个表中,并且UNION很棘手,因为它们没有相同的列。 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. 我已经尝试使用空值来使UNION工作,但是这给了我每个名称的重复行,而不是将它们分组为每个名称一行。

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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