gift_id 有100多种
gift_id,user_id 建立了索引
只需要找拥有某一gift_id的用户的查询如下,而且速度非常快
select * from user_gift where gift_id = 1004302 group by user_id
怎么快速找到 同时拥有 gift_id 为1004302和1004004的用户user_id呢 ?
阿神2017-04-17 16:51:00
select t.user_id, count(1) as c from table as t
where t.gift_id in(1004302, 1004004)
group by t.user_id
having count(1)>1
效率问题, 没数据, 也测试不了
如果, (user_id, gift_id) 是有可能重复的, 那在计算同时拥有之前还得将 (user_id, gift_id) 去重.
select t.user_id, count(1) as c from (
select user_id, gift_id from table group by user_id, gift_id
) as t
where t.gift_id in(1004302, 1004004)
group by t.user_id
having count(1)>1