我有两个表:
mysql> SELECT * FROM MasterListItemtype;
+----------------------+------------------------+
| MasterListItemtypeID | MasterListItemtypeName |
+----------------------+------------------------+
| 1 | Starters |
| 2 | Main Course |
| 3 | Side Course |
| 4 | Others |
| 5 | Desert |
+----------------------+------------------------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM MasterListMenuItems;
+------------------+-----------------------+-------+----------------------+
| MasterListMenuID | MasterListMenuName | Vegan | MasterListItemtypeID |
+------------------+-----------------------+-------+----------------------+
| 1 | Vada | 1 | 1 |
| 4 | Chinese Chicken Salad | 0 | 1 |
| 5 | Dosa Chicken Salad | 0 | 1 |
| 6 | Gobi Manchuri | 1 | 2 |
| 7 | Indian Salad | 1 | 3 |
| 8 | Gobi Chilli Manchuri | 1 | 2 |
| 9 | Chocolate IceCream | 1 | 5 |
| 10 | Vanilla IceCream | 1 | 5 |
+------------------+-----------------------+-------+----------------------+
8 rows in set (0.00 sec)
注意这里:
MasterListMenuID
是MasterListMenuItems
的主键MasterListItemtypeID
是MasterListItemtype
的主键MasterListItemtypeID
是MasterListMenuItems
的外键。
如何进行Mysql查询,以便:
我需要列出表
MasterListMenuName
的MasterListMenuItems
中的元素,这些元素是starters
最佳答案
尝试这个:
select MasterListMenuName from MasterListMenuItems where MasterListItemtypeID
=
(
select MasterListItemtypeID from MasterListItemtype where MasterListItemtypeName
= 'Starters')
(要么)
select m.MasterListMenuName from MasterListMenuItems m
join MasterListItemtype n
on m.MasterListItemtypeID = n.MasterListItemtypeID
and m.MasterListMenuName = 'Starters'