以下查询生成我要删除的记录列表...
SELECT *
FROM `orderitems` as oi
LEFT JOIN `orders` as o
ON (o.`orderid` = oi.`orderid`)
WHERE o.`date` = '2012-10-05'
AND o.`status` = 'init';
不幸的是,这些表的设计没有id
字段
我查看了其他带连接的删除操作,但都使用了id
个字段
这不起作用:
DELETE
FROM `orderitems` as oi
LEFT JOIN `orders` as o
ON (o.`orderid` = oi.`orderid`)
WHERE o.`date` = '2012-10-05'
AND o.`status` = 'init';
如何使用单个MySQL查询设置上述DELETE
? (没有PHP)
答案 0 :(得分:4)
试试这个,
DELETE oi -- add the alias here
FROM `orderitems` as oi
LEFT JOIN `orders` as o
ON (o.`orderid` = oi.`orderid`)
WHERE o.`date` = '2012-10-05'
AND o.`status` = 'init';
或使用子查询
DELETE FROM `orderitems`
WHERE `orderid` IN
(
SELECT o.`orderid`
FROM `orders` as o
WHERE o.`date` = '2012-10-05' AND o.`status` = 'init'
)