mysql 1kw數(shù)據(jù) 快速查詢
問題描述
gift_id 有100多種
gift_id,user_id 建立了索引
只需要找擁有某一gift_id的用戶的查詢?nèi)缦拢宜俣确浅?靤elect * 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
效率問題, 沒數(shù)據(jù), 也測試不了
如果, (user_id, gift_id) 是有可能重復(fù)的, 那在計(jì)算同時擁有之前還得將 (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
相關(guān)文章:
1. javascript - jquery選擇的dom元素如何更新?2. 視頻 - html5 video的autoplay 在智能手機(jī)上不運(yùn)作?3. javascript - H5頁面無縫輪播4. python - Django問題 ’WSGIRequest’ object has no attribute ’user’5. mysql輸入賬號密碼后跳出一大堆內(nèi)容后但卻進(jìn)不了mysql?6. javascript - nodejs中使用request庫怎么抓取網(wǎng)頁中的圖片7. mysql服務(wù)無法啟動1067錯誤,誰知道正確的解決方法?8. .......9. 數(shù)據(jù)庫 - mysql boolean型無法插入true10. python - flask jinjia2 中怎么定義嵌套變量
