mysql 1kw数据 快速查询
PHPz
PHPz 2017-04-17 16:49:00
0
2
641

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呢 ?

PHPz
PHPz

学习是最好的投资!

reply all(2)
大家讲道理

Check the user whose gift_id is 1004302 and save list1. Check the user whose gift_id is 1004004 and save list2. Find the intersection of the two lists

阿神
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

Efficiency issue, there is no data and it cannot be tested

If (user_id, gift_id) is likely to be duplicated, then (user_id, gift_id) must be deduplicated before calculating simultaneous possession.

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template