Detailed description of data instances in multiple tables in mySQL count, mysqlcount

Source: Internet
Author: User

Detailed description of data instances in multiple tables in mySQL count, mysqlcount

I. Implementation requirements

Recently, in the Achievement system, there are many dimensions related to the number of achievements. For example, the number of user fans reaches 50, the number of user likes reaches 100, and the number of likes reaches.

The number of fans isuser_relation Table

The number of likes isuser_praise Table

According to the previous practice, if you need to query the user's data, and because the data is in different tables, multiple SQL queries are often separated, for example:

SELECT count(*) mysh FROM user_relation WHERE other_uid =123456;
SELECT count(*) dzss FROM user_praise WHERE praise_uid = 123456;

Then, encapsulate the queried data in an object and return it to the client or another user. If the number of tables to be calculated is small, it is acceptable, but in the Achievement System, there are often a variety of achievements. We now involve 12 tables. If we follow this query, We need to write 12 SQL queries 12 times, which is a bit unpleasant.

2. Can I use an SQL statement?

The answer is yes.

We can useUNION Operation.

First, useUNION Rewrite the preceding statement:

SELECT wrzm,mysh,sgbh FROM ( SELECT count(*) wrzm,0 mysh,0 sgbh FROM user_witness WHERE plan_uid = 123456 UNION ALL SELECT 0 wrzm,count(*) mysh,0 sgbh FROM user_relation WHERE other_uid = 123456 UNION ALL SELECT 0 wrzm,0 mysh,count(*) sgbh FROM plan_stage WHERE uid = 123456 AND status = 1) t;

According to the above query results:


Mysql_count_results

We found that this result is already decent.wrzm ,mysh ,sgbh The three rows of data are changed to one row. So we can naturally think of usingSUM (Of course, I didn't expect it at the beginning, as a friend reminded me), so I changed the above SQL:

SELECT sum(wrzm) wrzm,sum(mysh) mysh,sum(sgbh) sgbh FROM ( SELECT count(*) wrzm,0 mysh,0 sgbh FROM user_witness WHERE plan_uid = 123456 UNION ALL SELECT 0 wrzm,count(*) mysh,0 sgbh FROM user_relation WHERE other_uid = 123456 UNION ALL SELECT 0 wrzm,0 mysh,count(*) sgbh FROM plan_stage WHERE uid = 123456 AND status = 1) t;

The result is as follows:


Mysql_count_result_2

At this point, we have obtained the expected results and implemented multiple tables in one SQL statement.count Statistics.

Iii. Expansion

We can only obtain user data, but what we need to calculate isuser_info It is also very simple for all users in. We will rewrite it again:

SELECT uid,sum(wrzm) wrzm,sum(mysh) mysh,sum(sgbh) sgbh FROM ( SELECT plan_uid uid,count(*) wrzm,0 mysh,0 sgbh FROM user_witness  GROUP BY plan_uid UNION ALL SELECT other_uid uid,0 wrzm,count(*) mysh,0 sgbh FROM user_relation  GROUP BY other_uid UNION ALL SELECT uid,0 wrzm,0 mysh,count(*) sgbh FROM plan_stage WHERE status = 1  GROUP BY uid) t GROUP BY uid;

The query result is:


Mysql_count_results_3

In this result, if we need to view a specific user, add
WHERE uid = 123456 You can simply addORDER BYYou can.

Summary

The above is all the content of this article. I hope the content of this article will be helpful for you when using or learning mysql. If you have any questions, please leave a message.

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.