Delete rows from two tables in one query

后端 未结 5 1037
[愿得一人]
[愿得一人] 2021-02-09 08:43

I have two tables: orders and orders_items. Both sharing the field orderID.

I want to delete all rows from both tables where orderID=500, but I need to do this in only o

5条回答
  •  眼角桃花
    2021-02-09 09:40

    Surely you can do that:

    DELETE FROM `table1`, `table2` WHERE `orderId` = 500
    

    see http://dev.mysql.com/doc/refman/5.0/en/delete.html

    [EDIT:]

    This is the whole trick:

    DELETE FROM `orders`, `orders_items` 
      USING `orders` 
      INNER JOIN `orders_items` ON `orders`.`orderId` = `orders_items`.`orderId` 
      WHERE `orders`.`orderId`= 500
    

    If orderId is a varchar, then change the statement to = '500'.

提交回复
热议问题