如何使用一个值从表中删除一行,其中表包含具有不同值的第二行?

时间:2017-09-08 19:24:01

标签: mysql sql database

鉴于以下表格数据:

+------+---------+----------+----------+-------------+------------+-----------------+---------------------+
| id   | version | quantity | state    | orderLineId | locationId | stockMoveTaskId | created             |
+------+---------+----------+----------+-------------+------------+-----------------+---------------------+
| 3277 |       0 |        2 | created  |      169108 |       5692 |            NULL | 2017-09-07 14:55:41 |
| 3073 |       0 |        2 | unpacked |      169108 |       5692 |            NULL | 2017-09-07 09:40:35 |
+------+---------+----------+----------+-------------+------------+-----------------+---------------------+

使用MySQL - 如何删除包含状态'创建的所有行?只有当表格中还包含一个状态为' unpacked'给定订单行ID?

1 个答案:

答案 0 :(得分:7)

您将使用delete。 。 。与join

delete todelete
    from t todelete join
         t
         on t.orderLineId = todelete.orderLineId
    where todelete.state = 'created' and t.state = 'unpacked';