SQL中如何删除指定查询出来的数据

2023-03-03 20:03:38 浏览数 (1)

我们知道,通过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 ''

)
sql

0 人点赞