gpt4 book ai didi

sql - 使用 InnerJoin Firebird 删除 STATEMENT

转载 作者:行者123 更新时间:2023-12-04 01:50:06 24 4
gpt4 key购买 nike

我做了这个选择语句来查找我想删除的重复行。我虽然只是将 SELECT 更改为 DELETE 并且它会删除行,但它没有。

这是选择语句:

select * FROM MYCARD T1
INNER JOIN( SELECT IDCARD, YEAR, MONEY FROM MYCARD GROUP BY IDCARD, YEAR, MONEY HAVING COUNT(IDCARD) > 1 ) T2 ON
T1.IDCARD = T2.IDCARD AND T1.YEAR=T2.YEAR
AND T1.MONEY = T2.MONEY
WHERE T1.IDMONEY = 5 AND IDCARD=80
AND ID not in (select min(ID)
from MYCARD
WHERE IDMONEY=5
AND IDCARD=80
group by IDCARD, YEAR);

我想使用这个删除:

DELETE FROM MYCARD T1
INNER JOIN( SELECT IDCARD, YEAR, MONEY FROM MYCARD GROUP BY IDCARD, YEAR, MONEY HAVING COUNT(IDCARD) > 1 ) T2 ON
T1.IDCARD = T2.IDCARD AND T1.YEAR=T2.YEAR
AND T1.MONEY = T2.MONEY
WHERE T1.IDMONEY = 5 AND IDCARD=80
AND ID not in (select min(ID)
from MYCARD
WHERE IDMONEY=5
AND IDCARD=80
group by IDCARD, YEAR);

但它不起作用。有什么想法吗?

我也试过这个:

DELETE FROM MYCARD
WHERE EXISTS (select * FROM MYCARD T1
INNER JOIN( SELECT IDCARD, YEAR, MONEY FROM MYCARD GROUP BY IDCARD, YEAR, MONEY HAVING COUNT(IDCARD) > 1 ) T2 ON
T1.IDCARD = T2.IDCARD AND T1.YEAR=T2.YEAR
AND T1.MONEY = T2.MONEY
WHERE T1.IDMONEY = 5 AND IDCARD=80
AND ID not in (select min(ID)
from MYCARD
WHERE IDMONEY=5
AND IDCARD=80
group by IDCARD, YEAR))

最佳答案

它不起作用,因为连接不是 Firebird delete syntax 的一部分.

例如,您需要使用带有 exists 的 delete

delete from mycard t1
where exists (
<a correlated subquery to identify the rows to delete>
)

或类似的方式来识别要删除的记录。底线是:所有必要的逻辑都需要在 where 子句中。

给定示例,您需要执行以下操作:

DELETE FROM MYCARD T1
WHERE T1.IDMONEY = 5 AND T1.IDCARD = 80
AND EXISTS (
SELECT IDCARD, YEAR, MONEY
FROM MYCARD T2
WHERE T2.IDCARD = T1.IDCARD
AND T2.YEAR = T1.YEAR
AND T2.MONEY = T1.MONEY
GROUP BY T2.IDCARD, T2.YEAR, T2.MONEY
HAVING COUNT(T2.IDCARD) > 1
)
AND T1.ID not in (
select min(ID)
from MYCARD
WHERE IDMONEY = 5
AND IDCARD = 80
group by IDCARD, YEAR
)

您可能需要在存在的选择中添加额外的 AND T2.IDMONEY = 5(除非 moneyidmoney 是同样的事情)。

关于sql - 使用 InnerJoin Firebird 删除 STATEMENT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40805629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com