简体   繁体   中英

Compare row name with one cell in sql

I have two tables. One table with a id, date, menu1, menu2, menu3 and another table with a id, id_date, number, menu_number.

In the second table there is data about users who have ordered a menu in a specific day. So example: ID: 1 ID_Date: 2014-09-09 Number: 4000 Menu_Number: Menu1. And with this Information I want to give out the Number, the date and the menu on this date which stands in the first table.

SELECT DISTINCT 
    B.ID, B.ID_Date, B.Menu, S.date 
FROM 
    ordering B, menu_plan S 
WHERE 
    B.ID_Date = S.Date 
    AND B.Number = '4000859'

This SQL statement only gives me the menu number for a specific day. But I want the content from the menu on this specific day from table 1. How does it work? Or should I do it in another way and write in the second table instead of the menu number the full menu?

SELECT DISTINCT 
    B.ID, B.ID_Date, 
    Menu =
        CASE S.menu_number
            WHEN 'Menu1' THEN B.menu1
            WHEN 'Menu2' THEN B.menu2
            WHEN 'Menu3' THEN B.menu3
            ELSE 'Unknown'
        END,
    S.date
FROM 
    ordering B
    JOIN menu_plan S ON B.ID_Date = S.Date 
WHERE 
    B.Number = '4000859'

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