MySQL Multiple tables merge a table to find

Source: Internet
Author: User

Today there is a requirement that MySQL multiple tables merge a table to find out how many userid (de-weight) the table has.

First of all, my idea is to combine multiple tables with union into a single table, then groupby, and finally sum (userid)to get the number.

Select COUNT(DISTINCT(`user_id`))
from (Select user_id fromlog_20160706Union Select user_id fromlog_20160707UnionSELECT user_id fromlog_20160708UNIONSELECT user_id fromlog_20160709UNIONSELECT user_id fromlog_20160710UNIONSELECT user_id fromlog_20160711UNIONSELECT user_id fromlog_20160712Order by user_id
) asTB//Here as must be extended to the entire section of select outside

It can be used, but it takes a long time if the table has a large amount of data.

Then think of another way of thinking, I can script from each table read out, count the players of each table, and then go to the weight (unique).

MySQL Multiple tables merge a table to find

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.