【发布时间】: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 之后)。我以为它会起作用...