在mysql中列出元素的简单查询

时间:2013-12-30 04:17:26

标签: mysql

我有两张桌子 ::

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)

请注意 ::

  • MasterListMenuIDMasterListMenuItems
  • 的主键
  • MasterListItemtypeIDMasterListItemtype
  • 的主键
  • MasterListItemtypeIDMasterListMenuItems

    的外键


如何制作Mysql查询以便 ::

我需要列出表MasterListMenuName MasterListMenuItemsstarters

的元素

4 个答案:

答案 0 :(得分:1)

SELECT * FROM MasterListMenuItems WHERE MasterListMenuItems.MasterListItemtypeID IN (SELECT MasterListItemtype.MasterListItemtypeID FROM MasterListItemtype WHERE MasterListItemtype. MasterListItemtypeName = 'Starters');

答案 1 :(得分:1)

试试这个

select MasterListMenuName from MasterListMenuItems mi 
    inner join MasterListItemtype it 
        on mi.MasterListItemtypeID = it.MasterListItemtypeID 
where it.MasterListItemtypeName = 'Starters'

答案 2 :(得分:1)

试试这个:

select MasterListMenuName from  MasterListMenuItems where MasterListItemtypeID 
 = 
 (  
select MasterListItemtypeID from MasterListItemtype where MasterListItemtypeName 
= 'Starters')

(OR)

select m.MasterListMenuName from  MasterListMenuItems m 
join  MasterListItemtype n
on m.MasterListItemtypeID = n.MasterListItemtypeID 
and m.MasterListMenuName = 'Starters'

答案 3 :(得分:0)

您只需使用外键作为启动器,在您的情况下为1。

SELECT * FROM MasterListMenuItems WHERE MasterListItemtypeID = 1