+-------+--------+
| EMPID | SALARY |
+-------+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
| 4 | 400 |
| 4 | 400 |
| 5 | 500 |
+-------+--------+
帮我查一下删除最后两行的查询。我已经尝试使用where where条件,但最后三行被删除,因为(4,400)是重复的。
答案 0 :(得分:1)
您可以设置
的限制DELETE `employee` ORDER BY `EMPID` DESC LIMIT 2
你可以添加你的where子句。在哪里EMPID =' 104',EMPID =' 105'
答案 1 :(得分:0)
尝试使用此查询:
WITH CTE AS(
SELECT [EMPID], [SALARY],
RN = ROW_NUMBER()OVER(PARTITION BY [EMPID], [SALARY] ORDER BY (SELECT 0))
FROM dbo.myTable
)
DELETE FROM CTE WHERE RN > 1
答案 2 :(得分:0)
试试这个。如果您想要所有重复记录,那么如果员工有重复工资,则会删除该员工,因为您说要删除两条记录。
WITH CTE
AS (SELECT *,
cnt= Count(1)OVER(PARTITION BY EMPID
ORDER BY (select null))
FROM yourTable)
DELETE FROM CTE
WHERE cnt > 1
答案 3 :(得分:0)
假设您要删除所述的最后两行(其中一行是重复的),这应该可以。
<强>查询强>
;WITH D as (
SELECT ROW_NUMBER() OVER(ORDER BY EMPID DESC) as rn,EMPID, SALARY
FROM YourTable
)
DELETE FROM D
WHERE rn <= 2;