你如何用JOIN和没有ID删除?

时间:2012-10-06 05:50:24

标签: mysql sql

以下查询生成我要删除的记录列表...

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)

1 个答案:

答案 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'
)