求助一条SQL语句,关于DELETE使用JOIN的 50
执行时报错#1064-YouhaveanerrorinyourSQLsyntaxSQL语句如下DELETEFROMc_worksWorksJOINc_works_atta...
执行时报错#1064 - You have an error in your SQL syntax
SQL语句如下
DELETE FROM c_works Works JOIN c_works_attach Works_attach ON Works_attach.wid=Works.id JOIN c_works_image Works_image ON Works_image.wid=Works.id WHERE ( Works.id = 1 ) 展开
SQL语句如下
DELETE FROM c_works Works JOIN c_works_attach Works_attach ON Works_attach.wid=Works.id JOIN c_works_image Works_image ON Works_image.wid=Works.id WHERE ( Works.id = 1 ) 展开
3个回答
展开全部
首先你必须得指明你删除哪个表中的数据
DELETE Works
FROM c_works Works JOIN c_works_attach Works_attach ON Works_attach.wid=Works.id JOIN c_works_image Works_image ON Works_image.wid=Works.id WHERE ( Works.id = 1 )
其次,sql server的话,得分开写,不能写到一起,你可以写一个tranction,例如
begin transaction;
declare @deletedIds table ( id int );
DELETE Works
output deleted.id into @deletedIds
FROM c_works Works JOIN c_works_attach
Works_attach ON Works_attach.wid=Works.id JOIN c_works_image Works_image
ON Works_image.wid=Works.id WHERE ( Works.id = 1 )
delete Works_attach
from c_works_attach as Works_attach
join @deletedIds d
on d.id = Works_attach.id;
delete Works_image
from c_works_image Works_image
join @deletedIds d
on d.id = Works_image.id;
commit transaction;
Wish it Helps
DELETE Works
FROM c_works Works JOIN c_works_attach Works_attach ON Works_attach.wid=Works.id JOIN c_works_image Works_image ON Works_image.wid=Works.id WHERE ( Works.id = 1 )
其次,sql server的话,得分开写,不能写到一起,你可以写一个tranction,例如
begin transaction;
declare @deletedIds table ( id int );
DELETE Works
output deleted.id into @deletedIds
FROM c_works Works JOIN c_works_attach
Works_attach ON Works_attach.wid=Works.id JOIN c_works_image Works_image
ON Works_image.wid=Works.id WHERE ( Works.id = 1 )
delete Works_attach
from c_works_attach as Works_attach
join @deletedIds d
on d.id = Works_attach.id;
delete Works_image
from c_works_image Works_image
join @deletedIds d
on d.id = Works_image.id;
commit transaction;
Wish it Helps
展开全部
这种语法在SQLSERVER中没问题,其他数据库没试
DELETE FROM Works from c_works Works JOIN c_works_attach Works_attach ON Works_attach.wid=Works.id JOIN c_works_image Works_image ON Works_image.wid=Works.id WHERE ( Works.id = 1 )
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
delete from 不能用 join
追问
请问有什么办法能一次性删除相关联的三个表的数据吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询