mysql 1kw數(shù)據(jù) 快速查詢
問(wèn)題描述
gift_id 有100多種
gift_id,user_id 建立了索引
只需要找擁有某一gift_id的用戶的查詢?nèi)缦拢宜俣确浅?靤elect * from user_gift where gift_id = 1004302 group by user_id
怎么快速找到 同時(shí)擁有 gift_id 為1004302和1004004的用戶user_id呢 ?
問(wèn)題解答
回答1:查 gift_id 為1004302的用戶存list1 查 gift_id 為1004004的用戶存list2 兩個(gè)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
效率問(wèn)題, 沒數(shù)據(jù), 也測(cè)試不了
如果, (user_id, gift_id) 是有可能重復(fù)的, 那在計(jì)算同時(shí)擁有之前還得將 (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. win下面的cmder中的vim . 中文亂碼. 試了百度上的各種解決方式.. 還是沒弄好2. PHP中的$this代表當(dāng)前的類還是方法?3. win10系統(tǒng) php安裝swoole擴(kuò)展4. node.js - 為什么npm安裝vue-cli會(huì)出現(xiàn)下面的錯(cuò)誤??!!!?5. 前端 - IE9 css兼容問(wèn)題6. css3 content icon7. javascript - 關(guān)于ios微信端瀏覽器網(wǎng)頁(yè)的一些問(wèn)題8. css3 - CSS偽類選擇器,如何選擇并控制相鄰的上一個(gè)標(biāo)簽?9. javascript - bootstrap table固定列之后寬度無(wú)法對(duì)齊怎么解決?10. css - 如何讓圖片像雲(yún)一樣的行為?
