mysql多張表合并一張表進行尋找

來源:互聯網
上載者:User

標籤:

  今天有個需求是 mysql多張表合并一張表查詢出該表有多少個userid(去重)。

首先我的思路是,把多張表採用union合并成一張表,然後進行group by, 最後進行sum(userid), 得出數量。

select COUNT(DISTINCT(`user_id`)) 
FROM (select user_id from log_20160706union select user_id from log_20160707unionSELECT user_id from log_20160708UNIONSELECT user_id from log_20160709UNIONSELECT user_id from log_20160710UNIONSELECT user_id from log_20160711UNIONSELECT user_id from log_20160712order by user_id
) as TB // 這裡as一定要擴在整段select外面

可以使用,不過如果表資料量大的話,花費時間還是蠻長的。

後來想想還有一種思路,我可以指令碼從每張表讀出來,統計出每張表的玩家,然後進行去重(unique)一下。

 

mysql多張表合并一張表進行尋找

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.