SQL DELETE with INNER JOIN

前端 未结 3 463
慢半拍i
慢半拍i 2020-12-02 17:39

There are 2 tables, spawnlist and npc, and I need to delete data from spawnlsit. npc_templateid = n.idTemplate is the onl

3条回答
  •  独厮守ぢ
    2020-12-02 18:37

    If the database is InnoDB then it might be a better idea to use foreign keys and cascade on delete, this would do what you want and also result in no redundant data being stored.

    For this example however I don't think you need the first s:

    DELETE s 
    FROM spawnlist AS s 
    INNER JOIN npc AS n ON s.npc_templateid = n.idTemplate 
    WHERE n.type = "monster";
    

    It might be a better idea to select the rows before deleting so you are sure your deleting what you wish to:

    SELECT * FROM spawnlist
    INNER JOIN npc ON spawnlist.npc_templateid = npc.idTemplate
    WHERE npc.type = "monster";
    

    You can also check the MySQL delete syntax here: http://dev.mysql.com/doc/refman/5.0/en/delete.html

提交回复
热议问题