【问题标题】:How to delete rows in Table1 where COUNT of WHERE in Table2 is greater than 0?如何删除Table1中Table2中WHERE的COUNT大于0的行?
【发布时间】:2016-11-25 09:35:50
【问题描述】:

我有带有用户 ID 的 [Table1] 和带有相同用户 ID(左连接)的 [Table2] 和带有已读消息数量的“打开”列。我想从 [Table1] 中删除所有用户,这些用户在 [Table2] 中具有“打开”列值

尝试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;

【问题讨论】:

  • 第一个查询没有 FROM。
  • 第二个查询需要一个子查询来进行计数。
  • 这个“开放”专栏的本质是什么?它是 int 还是需要计数?你说的好像它是列中的一个值(“where open
  • @jarlh,因为我在“.*”之前指定了表(在 SELECT 之后)。我以为它会起作用...

标签: mysql sql count left-join


【解决方案1】:

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

我对您的逻辑的理解是,您想从 [Table1] 中删除 [Table2] 中没有任何行的所有用户,其中 open &gt; 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 )

【讨论】:

  • 没关系,我删除了两个表中的行,但是我还需要删除当前(userstats)表中的行:DELETE from pg_acymailing_userstats where subid not in ( select subid from pg_acymailing_userstats where open &gt; 0 );,它给了我错误:1093 - 表 'pg_acymailing_userstats' 是指定了两次,既作为“DELETE”的目标,又作为数据的单独源
  • 已解决! DELETE from pg_acymailing_userstats where pg_acymailing_userstats.subid not in (select * from (select subid from pg_acymailing_userstats where open &gt; 0) t);
【解决方案2】:

如果你想过滤聚合函数的结果,你需要 GROUP BY 和 HAVING 子句,例如

SELECT pg_acymailing_subscriber.userid,
       COUNT(DISTINCT case when pg_acymailing_userstats.open > 0 end) as readc
FROM `pg_acymailing_subscriber`
LEFT JOIN `pg_acymailing_userstats`
    ON pg_acymailing_subscriber.subid=pg_acymailing_userstats.subid
WHERE pg_acymailing_subscriber.subid=24
GROUP BY pg_acymailing_subscriber.userid
HAVING readc > 0;

我猜这个用户ID列叫做userid。我的引用可能不正确,但这很容易修复。

HAVING readc &gt; 0 在 mysql 中可能是正确的语法,也可能不是正确的语法。 如果不是,你写HAVING COUNT(DISTINCT case when pg_acymailing_userstats.open &gt; 0 end) &gt; 0

【讨论】:

    【解决方案3】:

    如果我理解正确,你的问题是你想删除 table1 中与 table2 中的 id 相关的行,而所有打开消息的总和是 这应该是选择

      select table1.userID
      from table1 
      inner  join table2 on table1.userID = table2.userID 
      group by table1.userID
      havine sum(open)<1
    

    然后你可以使用像

    这样的删除
      detele 
      from table1 
      inner  join table2 on table1.userID = table2.userID 
      group by table1.userID
      havine sum(open)<1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多