Home  >  Q&A  >  body text

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

PHPzPHPz2742 days ago627

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 16:51:00

    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

    reply
    0
  • 阿神

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

    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

    reply
    0
  • Cancelreply