gpt4 book ai didi

mysql - 如何删除 Table1 中 Table2 中 WHERE 的 COUNT 大于 0 的行?

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:33 26 4
gpt4 key购买 nike

我有带用户 ID 的 [Table1] 和带相同用户 ID(左连接)的 [Table2] 以及带有已读消息数的“打开”列。我想从 [Table1] 中删除所有用户,其中 [Table2] 中的所有行的“打开”列值 < 1。我试图先选择它们,但它给出了 SQL 错误:

尝试 1:

SELECT pg_acymailing_subscriber.*,
COUNT(DISTINCT case when pg_acymailing_userstats.open > 0 end) as readc
LEFT JOIN `pg_acymailing_userstats`
ON pg_acymailing_subscriber.subid=pg_acymailing_userstats.subid
WHERE pg_acymailing_subscriber.subid=24 AND readc > 0;

尝试 2:

SELECT *
FROM `pg_acymailing_subscriber`
LEFT JOIN `pg_acymailing_userstats`
ON pg_acymailing_subscriber.subid=pg_acymailing_userstats.subid
WHERE COUNT(DISTINCT case when pg_acymailing_userstats.open > 0 /*If user doesn't have rows with "open" column value > 0, we select it if whole rows numer is 0*/
end) < 1
and pg_acymailing_subscriber.subid=24;

最佳答案

如果性能不是问题,使用子查询构建它可能是最简单的。

我对您的逻辑的理解是,您想从 [Table1] 中删除所有在 [Table2] 中没有任何行的用户,其中 open > 0,这是对您要求的正确解释吗?

select * from pg_acymailing_subscriber where subid not in
( select subid from pg_acymailing_userstats where open > 0 )

如果显示正确的记录,删除如下:

delete from pg_acymailing_subscriber where subid not in
( select subid from pg_acymailing_userstats where open > 0 )

关于mysql - 如何删除 Table1 中 Table2 中 WHERE 的 COUNT 大于 0 的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40801715/

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