MySQL 查询多个表并且 return 字段在找不到 id 时为 null

MySQL query multiple tables and return fields as null when id not found

我对 MySQL 查询很陌生,我正在尝试从多个 table 中执行大型 SQL 查询。我有三个 tables,客户、订单和食物。它们类似于以下内容:

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'

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

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 

然而,当其中一个 table 中不存在 id 时,整个 return 就是空集。在这种情况下,我只需要用 NULL 填充所需的字段。我还需要防止客户 table 中的 order_id 为 NULL,例如查找 id 4 时的输出。

首先,您需要在顾客 table 与食物和物品之间建立关系。

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

那么查询可以这样修改

SELECT customer.order_id, customer.purchase_category, order.payment_method, food.type, item.product 来自客户 左加入食物 customer.food_type_id = food.id 左加入项目 customer.item_type_id = item.id INNER JOIN 命令 ON customer.order_id=order.order_id 其中 customer.id=1 且 food.id=1 且 item.id=1

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

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 秒)