我试图删除一个简单表中除了id最高的副本之外具有重复值的所有行。
表:
CREATE TABLE IF NOT EXISTS [Expression] (
[ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[Value] VARCHAR(2048) NOT NULL
)
尝试查询:
DELETE Expression
WHERE EXISTS (
SELECT 1
FROM Expression Exp2
WHERE Expression.Value=Exp2.Value
AND Expression.Id < Exp2.Id)
失败,出现“SQL逻辑错误或数据库丢失”表达式“:语法错误”
DELETE Exp1
FROM Expression Exp1
INNER JOIN Expression Exp2
ON Exp1.Value=Exp2.Value AND Exp1.Id < Exp2.Id
失败,出现“SQL逻辑错误或数据库丢失”Exp1“:语法错误”
我需要使用哪种语法?
答案 0 :(得分:2)
不要忘记 FROM
。
DELETE FROM Expression
WHERE EXISTS (
SELECT 1
FROM Expression Exp2
WHERE Expression.Value=Exp2.Value
AND Expression.Id < Exp2.Id
);
答案 1 :(得分:2)
您可以通过多种方式实现这一目标:
1)使用CTE:
WITH CTE AS (
SELECT ID, VALUE, ROW_NUMBER() OVER(ORDER BY Value) as RowNum
FROM Expression
)
DELETE
FROM CTE
WHERE ROWNum >1
2)使用临时表:相同的概念
答案 2 :(得分:0)
从联系人t1的内部联接中删除t1 t1.id