使用另一个表中的查询删除一个表中的行

时间:2015-10-05 23:44:18

标签: mysql sql-server

我的数据库中有2个表。

表客户ID,firstName,lastName,地址......

和包含id,idCustomer ......的表命令。

我要删除名字为“john”的客户的所有订单

我如何编写命令?

感谢

2 个答案:

答案 0 :(得分:3)

DELETE o
FROM        Orders   o
INNER JOIN  Customer c ON o.idCustomer = c.ID
WHERE       c.firstName = 'john'

答案 1 :(得分:0)

Delete From
    orders 
Where
    Orders.firstName In 
        (Select
            firstName 
        From
            customers
            Inner Join orders On customers.ID = orders.idCustomer
        Where
            customers.firstName = "john")