简体   繁体   English

删除表MYSQL中的多个字段的SQL语句

[英]SQL statement to delete multiple fields in a table MYSQL

I could finally solve the DELETE CASCADE. 我终于可以解决DELETE CASCADE了。 for these tables. 这些表。

http://subefotos.com/ver/?a6f811bfb78e9aa78ffe43adb1f3cf5do.png

Now... I need delete all row on AlergiaTipo when these seems related to a alergiagrupo on the table ALERGIA. 现在...当这些似乎与ALERGIA桌上的alergiagrupo有关时,我需要删除AlergiaTipo上的所有行。 I make this statement. 我发表这个声明。

delete from alergiatipo where ID in (select alergiatipo.ID from alergia,alergiatipo where
alergia.AlergiaTipo_ID = alergiatipo.iD and alergia.ID in (select alergia.ID from 
alergia,alergiagrupo where alergia.AlergiaGrupo_ID = alergiagrupo.ID AND 
alergiagrupo.ID = '2'));

but return me this error 但是给我这个错误

Error Code: 1093. You can't specify target table 'alergiatipo' for update in FROM clause

This is your delete with proper explicit join syntax: 这是使用正确的显式join语法进行的delete

delete from alergiatipo
    where ID in (select alergiatipo.ID
                 from alergia join
                      alergiatipo
                      on alergia.AlergiaTipo_ID = alergiatipo.iD
                 where alergia.ID in (select alergia.ID 
                                      from alergia join
                                           alergiagrupo 
                                           on alergia.AlergiaGrupo_ID = alergiagrupo.ID 
                                      where alergiagrupo.ID = '2'
                                     )
               );

In MySQL, you cannot specify the table being deleted elsewhere in an update or delete . 在MySQL中,您不能在updatedelete指定要删除的表。 Plus, your delete is rather overdone. 另外,您的删除操作过高。 I think you just want the following: 我认为您只需要以下内容:

delete alt
    from alergiatipo alt join
         alergia a
         on a.AlergiaTipo_ID = alt.iD join
         alergiagrupo ag
         on a.AlergiaGrupo_ID = ag.ID
    where ag.ID = 2;

I also removed the single quotes around 2 . 我还删除了2附近的单引号。 I'm guessing the column is an integer, so you should use an integer constant rather than a string constant. 我猜该列是整数,因此您应该使用整数常量而不是字符串常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM