我们知道,通过Delete From [xxx] where a=x 可以删除数据,那么如何删除通过查询出来的数据呢?
示例
w3resources - SQL Delete records using subqueries
代码语言:javascript复制DELETE FROM customer1
WHERE agent_code=ANY(
SELECT agent_code FROM agents
WHERE working_area='London');
那么我们如何删除通过查询发现对比两个查询中的不一致的呢?
SQL Compare A ∪ B : UNION or UNION ALL (UNION eliminates duplicates, UNION ALL keeps them) A ∩ B : INTERSECT A − B : EXCEPT
代码语言:javascript复制SELECT CustId, CustName, CustAddress, CustPhone FROM Original
EXCEPT
SELECT CustId, CustName, CustAddress, CustPhone FROM Revised
所以当我们想要删除通过查询对比出不一致的数据,应该
代码语言:javascript复制ELETE FROM customer1
WHERE agent_code=ANY(
Select UserID
From Original
Where UserID not like '[0-9]%'
EXCEPT
SELECT
UserID
FROM Revised as cs
Left join [TTID] on cs.CustId = TTID.CustId
where cs.UserID not like ''
)