SQL DELETE WITH JOIN不起作用

时间:2013-03-23 23:45:25

标签: sql select

我有这个查询,它返回没有引用的关键字的所有记录 查询正在运行,但我想删除所有这些记录,问题是如果我把DELETE而不是SELECT我得到错误。

SELECT Keywords.[Id]
  ,[QuoteId]      
  FROM [QuotesTemple].[dbo].[Keywords]
 LEFT JOIN [QuotesTemple].[dbo].Quotes ON Keywords.QuoteId=Quotes.Id
  WHERE Quotes.Id IS NULL

这不起作用。

DELETE
  FROM [QuotesTemple].[dbo].[Keywords]
  LEFT JOIN [QuotesTemple].[dbo].Quotes ON Keywords.QuoteId=Quotes.Id
  WHERE Quotes.Id IS NULL

我收到此错误:

第15行,第1行,第4行 关键字“LEFT”附近的语法不正确。

3 个答案:

答案 0 :(得分:2)

像这样写:

DELETE [QuotesTemple].[dbo].[Keywords] FROM [QuotesTemple].[dbo].[Keywords]
  LEFT JOIN [QuotesTemple].[dbo].Quotes ON Keywords.QuoteId=Quotes.Id
  WHERE Quotes.Id IS NULL

答案 1 :(得分:1)

有效语法为:

DELETE [QuotesTemple].[dbo].[Keywords]
FROM [QuotesTemple].[dbo].[Keywords] AS k
LEFT JOIN [QuotesTemple].[dbo].[Quotes] AS q ON k.QuoteId = q.Id
WHERE q.Id IS NULL

答案 2 :(得分:1)

you can also write as below

delete t1 FROM projects AS t1 LEFT OUTER JOIN [QuotesTemple].[dbo].Quotes AS t2 on t1.QuoteId= t2.QuoteId where t2.Id is Null