mysql 1kw数据 快速查询
问题描述
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呢 ?
问题解答
回答1:查 gift_id 为1004302的用户存list1 查 gift_id 为1004004的用户存list2 两个list取交集
回答2:select t.user_id, count(1) as c from table as twhere t.gift_id in(1004302, 1004004)group by t.user_idhaving 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 twhere t.gift_id in(1004302, 1004004)group by t.user_idhaving count(1)>1
相关文章:
1. javascript - 数组原声方法中的一段代码2. python小白的基础问题 关于while循环的嵌套3. MySQL客户端吃掉了SQL注解?4. javascript - JS设置Video视频对象的currentTime时出现了问题,IE,Edge,火狐,都可以设置,反而chrom却...5. 求大神帮我看看是哪里写错了 感谢细心解答6. javascript - 百度echarts series数据更新问题7. python - Django分页和查询参数的问题8. javascript - 图片能在网站显示,但控制台仍旧报错403 (Forbidden)9. php自学从哪里开始?10. phpstady在win10上运行
