sql求助

用户行为表

id uid event
1 1 e1
2 1 e2
3 1 e3
4 1 e3
5 2 e3
6 2 e4

事件 e1,e2,e3,e4
如何统计出【有且仅有】完成3件【任意事件】的 用户人数?
求大佬提供一下sql思路

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

我试了一下, 楼上的回答没有问题。

DROP TABLE `test`;
CREATE TABLE `test` (
  `id` bigint(5) NOT NULL AUTO_INCREMENT,
  `uid` bigint(20) DEFAULT NULL,
  `event` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4;

INSERT INTO test (`uid`, `event`) VALUES (1, 'test_a');
INSERT INTO test (`uid`, `event`) VALUES (1, 'test_b');
INSERT INTO test (`uid`, `event`) VALUES (1, 'test_c');
INSERT INTO test (`uid`, `event`) VALUES (1, 'test_d');
INSERT INTO test (`uid`, `event`) VALUES (2, 'test_a');
INSERT INTO test (`uid`, `event`) VALUES (2, 'test_b');
INSERT INTO test (`uid`, `event`) VALUES (2, 'test_d');
INSERT INTO test (`uid`, `event`) VALUES (3, 'test_a');
INSERT INTO test (`uid`, `event`) VALUES (3, 'test_b');
INSERT INTO test (`uid`, `event`) VALUES (3, 'test_b');
INSERT INTO test (`uid`, `event`) VALUES (4, 'test_a');
INSERT INTO test (`uid`, `event`) VALUES (4, 'test_b');
INSERT INTO test (`uid`, `event`) VALUES (5, 'test_a');

SELECT COUNT(uid) as `query_a`
FROM (
  SELECT uid, COUNT(event) AS c
  FROM test
  GROUP BY uid
  HAVING c = 3
) as b;

SELECT COUNT(uid) as `query_b`
FROM (
  SELECT uid, COUNT(DISTINCT event) AS c
  FROM test
  GROUP BY uid
  HAVING c = 3
) as b;
TRUNCATE TABLE `test`;
5个月前 评论
徵羽宫 (作者) 5个月前
讨论数量: 9
select count(*) from (select count(*) as uc from table group by uid having uc = 3) as t;
5个月前 评论
pwz857409056 5个月前

完成了 e1,e3,e3 ,这样算两件还是一件

算两件的话

SELECT COUNT(DISTINCT uid)
FROM (
  SELECT uid, COUNT(DISTINCT event) AS c
  FROM user_events
  GROUP BY uid
  HAVING c = 3
) as b;

算三件就不用去重

SELECT COUNT(uid) 
FROM (
  SELECT uid, COUNT(DISTINCT event) AS c
  FROM user_events
  GROUP BY uid
  HAVING c = 3
) as b;
5个月前 评论
SELECT COUNT(DISTINCT uid)  
FROM (  
  SELECT uid, COUNT(event) AS event_count  
  FROM 行为表  
  GROUP BY uid  
  HAVING COUNT(DISTINCT event) = 3  
) subquery;
5个月前 评论

分组去重

5个月前 评论

我试了一下, 楼上的回答没有问题。

DROP TABLE `test`;
CREATE TABLE `test` (
  `id` bigint(5) NOT NULL AUTO_INCREMENT,
  `uid` bigint(20) DEFAULT NULL,
  `event` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4;

INSERT INTO test (`uid`, `event`) VALUES (1, 'test_a');
INSERT INTO test (`uid`, `event`) VALUES (1, 'test_b');
INSERT INTO test (`uid`, `event`) VALUES (1, 'test_c');
INSERT INTO test (`uid`, `event`) VALUES (1, 'test_d');
INSERT INTO test (`uid`, `event`) VALUES (2, 'test_a');
INSERT INTO test (`uid`, `event`) VALUES (2, 'test_b');
INSERT INTO test (`uid`, `event`) VALUES (2, 'test_d');
INSERT INTO test (`uid`, `event`) VALUES (3, 'test_a');
INSERT INTO test (`uid`, `event`) VALUES (3, 'test_b');
INSERT INTO test (`uid`, `event`) VALUES (3, 'test_b');
INSERT INTO test (`uid`, `event`) VALUES (4, 'test_a');
INSERT INTO test (`uid`, `event`) VALUES (4, 'test_b');
INSERT INTO test (`uid`, `event`) VALUES (5, 'test_a');

SELECT COUNT(uid) as `query_a`
FROM (
  SELECT uid, COUNT(event) AS c
  FROM test
  GROUP BY uid
  HAVING c = 3
) as b;

SELECT COUNT(uid) as `query_b`
FROM (
  SELECT uid, COUNT(DISTINCT event) AS c
  FROM test
  GROUP BY uid
  HAVING c = 3
) as b;
TRUNCATE TABLE `test`;
5个月前 评论
徵羽宫 (作者) 5个月前

我选择增加冗余字段,记录完成事件数或者增加表,记录每个用户每个事件完成次数,单纯靠mysql统计不大行

5个月前 评论
寞小陌 (楼主) 5个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!