这是学生表:
UID NAME SPECIALTY
123456 John Writing
123456 John NULL
234567 Jane Acting
234567 Jane NULL
345678 Bob Writing
345678 Bob NULL
我想运行一个查询,它将删除所有具有重复UID和NULL专业的记录。我正在使用ColdFusion,MSQL 2012。
谢谢。
答案 0 :(得分:3)
你能做点什么:
delete
from students s1
where specialty is null
and exists(select 1
from students s2
where specialty is not null
and s2.uid = s1.uid);
这是Oracle中的相关子查询。不确定你的环境。
答案 1 :(得分:1)
在MS-SQL服务器中尝试以下查询,
DELETE FROM
students
WHERE
specialty IS NULL
AND UID IN (
SELECT
UID
FROM
students
GROUP BY
UID
HAVING
COUNT(*) > 1
)