我想进行以下查询:
按特定属性计算人数,但仅限于他们在我的网站上执行了特定操作。
按属性计算人数:
select attribute, count(*) from users group by attribute
现在,特定操作存储在表Y中。
所以我只想在这个特定用户在表Y中有一个条目时执行计数。用户将按id存储。所以基本上我想做的是;
select attribute, count(*) from users group by attribute IF Y has user_id of user
答案 0 :(得分:0)
正如Barmar指出的那样,如果我们使用简单的连接,我们不希望出现多行。 因此,我们建立一个临时表来加入不同的ID。
select attribute, count(*) from users as u,
(select distinct user_id from Y) as Y2
where U.user_id = Y2.user_id
group by attribute;
答案 1 :(得分:0)
SELECT attribute, COUNT(*)
FROM USERS AS u
JOIN (SELECT DISTINCT user_id
FROM Y) AS y
ON u.user_id = y.user_id
GROUP BY attribute
或:
SELECT attribute, COUNT(*)
FROM users
WHERE user_id in (SELECT user_id FROM y)
GROUP BY attribute