我已经从我在本网站上看到的内容中尝试了很多答案,包括“不存在的地方”和“#39;条款。这是我正在处理的原始SQL
INSERT INTO tbl_Feed
(FeedTypeID, CustomerID, Name, Code, Color, Icon, FeedTypeSortOrder, IsUsingEmailAddress, IsActive)
select DISTINCT
3, c.CustomerID, "Unattached Image", "Unattached Image", "0x0093D0", "offline_01.png", COALESCE((select MAX(FeedTypeSortOrder) as FeedTypeSortOrderMax from tbl_Feed where FeedTypeID = 3 and customerid=c.CustomerID) + 1, 1), 0, 1
from tbl_Customer c
join tbl_Feed f on f.customerid = c.customerid
where f.code != "Unattached Image"
如果我运行两次,它会进入另一行" Unattached Image"。我希望它只能连接不具有此值的表...我想确保将where子句更改为where not exist(select name from tbl_Feed where name != "Unattached Image")
但更新了0行。请指教。
答案 0 :(得分:2)
您需要使用NOT EXISTS:
INSERT INTO tbl_Feed
(FeedTypeID, CustomerID, Name, Code, Color, Icon, FeedTypeSortOrder, IsUsingEmailAddress, IsActive)
select DISTINCT
3, c.CustomerID, 'Unattached Image', 'Unattached Image', '0x0093D0',
'offline_01.png', COALESCE((select MAX(FeedTypeSortOrder) as FeedTypeSortOrderMax from tbl_Feed where FeedTypeID = 3 and customerid=c.CustomerID) + 1, 1), 0, 1
from tbl_Customer c
join tbl_Feed f on f.customerid = c.customerid
where NOT EXISTS(select 1 from tbl_Feed where name = 'Unattached Image' AND CustomerID = c.CustomerID)