如何删除mySQL中为NULL的LEFT JOIN行?

时间:2018-04-18 15:16:29

标签: mysql join

我正在做这样的LEFT JOIN

SELECT mt_order_delivery_address.id, 
       mt_order.order_id, 
       mt_order_delivery_address.client_id
FROM mt_order_delivery_address
LEFT JOIN mt_order ON mt_order.order_id = mt_order_delivery_address.order_id

然后给我一个列表:

id     order_id    client_id
----------------------------
1032   NULL        382
1028   NULL        282
1020   784344      920
1002   232496      490

我想摆脱mt_order_delivery_addressorder_id中没有对应mt_order的行。我该怎么做?我似乎无法绕过它。

更新:

我试过这个,但似乎不起作用:

DELETE mt_order_delivery_address.id FROM mt_order_delivery_address LEFT JOIN mt_order ON mt_order.order_id = mt_order_delivery_address.order_id WHERE mt_order.order_id IS NULL

1 个答案:

答案 0 :(得分:1)

如果通过“删除”表示“不在此查询结果中显示它们”,则可以使用INNER JOIN而不是LEFT JOIN。值得学习不同类型的连接以及如何使用它们。

如果“删除”是指“DELETE my_order_delivery_address中没有匹配my_order的行”,则可以执行multi-table DELETE

DELETE a FROM mt_order_delivery_address AS a
LEFT JOIN mt_order AS o USING (order_id)
WHERE o.order_id IS NULL;