当查找未找到id时,MySQL查询多个表并将字段返回为null

时间:2015-09-28 15:04:51

标签: mysql null left-join where

我是MySQL查询的新手,我正在尝试从多个表中执行大型SQL查询。我有三张桌子,顾客,订单和食物。它们类似于以下内容:

customer
id  |  order_id  | purchase_category
----+------------+------------------
1   |  123       |  'Sandwich'
2   |  456       |  'Item'
3   |  789       |  'Dessert'
4   |  NULL      |  'Item'


order
order_id  |  payment_method
---------------------------
123       |  'credit_card'
456       |  'debit_card'

food
id  |  type
-----------
1   |  'Burger'
3   |  'Cake'

item
id  |  product
--------------
2   |  'Stickers'
4   |  'Game'

我试图通过id找到字段。如果查找id 1:

,我希望SQL查询的输出是这样的
order_id  |  purchase_category  |  payment_method  |  type    |  product
----------------------------------------------------------------------
123       |  'Sandwich'         |  'credit_card'   | 'Burger' |  NULL

如果要找id 2:

order_id  |  purchase_category  |  payment_method  |  type    |  product
----------------------------------------------------------------------
456       |  'Item'             |  'debit_card     | NULL     |  'Stickers'

如果要找id 3:

order_id  |  purchase_category  |  payment_method  |  type    |  product
----------------------------------------------------------------------
789       |  'Dessert'          |  NULL            | 'Cake'   |  NULL

如果要找id 4:

order_id  |  purchase_category  |  payment_method  |  type    |  product
----------------------------------------------------------------------
NULL      |  'Item'             |  NULL            |  NULL    |  'Game'

请注意表中的ID可能不存在于表中,但如果表中不存在ID,我仍希望将所需字段返回为NULL。我已经进行了广泛的研究,试图找到解决方案,我似乎无法得到它。这是我到目前为止编写的代码:

SELECT customer.order_id, customer.purchase_category, order.payment_method, food.type, item.product
FROM customer, food, item
LEFT JOIN order
ON customer.order_id=order.order_id
WHERE customer.id=1 and food.id=1 and item.id=1 

但是当其中一个表中不存在id时,整个事件返回空集。在这种情况下,我只需要填充NULL所需的字段。当客户表中的order_id为NULL时,我还需要防范,例如寻找id 4时的输出。

2 个答案:

答案 0 :(得分:1)

SELECT customer.order_id, customer.purchase_category, 
order.payment_method, food.type, item.product 
FROM customer 
LEFT JOIN order ON customer.order_id = order.id 
LEFT JOIN food ON customer.id = food.id 
LEFT JOIN item ON customer.id = item.id                 

如上所述,您应该使用LEFT JOIN。

+----------+-------------------+----------------+--------+----------+
| order_id | purchase_category | payment_method | type   | product  |
+----------+-------------------+----------------+--------+----------+
|      123 | Sandwich          | credit_card    | Burger | NULL     |
|      456 | Item              | debit_card     | NULL   | Stickers |
|      789 | Dessert           | NULL           | Cake   | NULL     |
+----------+-------------------+----------------+--------+----------+

3行(0.00秒)

答案 1 :(得分:0)

首先,您需要在客户餐桌与食品和商品之间建立关系。

如果是1对1关系,则在customer表中可以添加food_type_id和item_type_id两个字段默认为NULL。

然后可以像这样修改查询

SELECT customer.order_id,customer.purchase_category,order.payment_method,food.type,item.product 来自客户    LEFT JOIN食品ON customer.food_type_id = food.id    LEFT JOIN项目在customer.item_type_id = item.id上 内部联合订单    ON customer.order_id = order.order_id customer.hid = 1,food.id = 1,item.id = 1

在此处加入图表:sql joins as venn diagram 看看这里的关系:How to create relationships in MySQL