How to delete from multiple tables in MySQL?

后端 未结 7 843
温柔的废话
温柔的废话 2020-11-22 07:57

I am trying to delete from a few tables at once. I\'ve done a bit of research, and came up with this

DELETE FROM `pets` p,
            `pets_activities` pa
          


        
7条回答
  •  太阳男子
    2020-11-22 08:46

    Since this appears to be a simple parent/child relationship between pets and pets_activities, you would be better off creating your foreign key constraint with a deleting cascade.

    That way, when a pets row is deleted, the pets_activities rows associated with it are automatically deleted as well.

    Then your query becomes a simple:

    delete from `pets`
        where `order` > :order
          and `pet_id` = :pet_id
    

提交回复
热议问题