[英]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.
一个表具有id,日期,menu1,menu2,menu3,另一表具有id,id_date,数字,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.
例如:ID:1 ID_日期:2014-09-09数字: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. 该SQL语句仅提供特定日期的菜单号。 But I want the content from the menu on this specific day from table 1. How does it work?
但是,我希望从表1的特定日期开始菜单中的内容。它是如何工作的? 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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.