MySQL implements multi-table association statistics (subquery statistics) example

MySQL implements multi-table association statistics (subquery statistics) example

This article uses an example to describe how to implement multi-table association statistics in MySQL. Share with you for your reference, the details are as follows:

need:

Statistics on the reward amount for each book, recharge data statistics at different times, consumption statistics,

Design four tables, book table, orders table, reward_log table, consume_log table, and associate them with book table through book_id.

question:

When more than two tables are associated, data duplication occurs during statistics. You have to use a subquery to find it out. A subquery can only query one field. Here, the CONCAT_WS function is used to concatenate multiple fields.

accomplish:

The query code is as follows

SELECT
b.id,
b.book_name,
sum( IF ( o.create_time > 0 && o.create_time < 9999999999, o.price, 0 ) ) today_pay_money,
sum( IF ( o.create_time > 0 && o.create_time < 9999999999, 1, 0 ) ) today_pay_num,
sum( IF ( o.create_time > 999 && o.create_time < 9999, o.price, 0 ) ) yesterday_pay_money,
sum( IF ( o.create_time > 999 && o.create_time < 9999, 1, 0 ) ) yesterday_pay_num,
sum(o.price) total_pay_money,
sum( IF ( o.create_time > 9999 && o.create_time < 99999, 1, 0 ) ) total_pay_num,
( SELECT SUM( total_score ) FROM book_reward_log WHERE book_id = b.id ) total_score,
(
 SELECT
 CONCAT_WS(
  ',',
  SUM( IF ( create_time > 0 && create_time < 998, score, 0 ) ),
  SUM( IF ( create_time > 9999 && create_time < 99998, score, 0 ) ),
  SUM( IF ( create_time > 99999 && create_time < 999998, score, 0 ) )
 )
 FROM
 book_consume_log
 WHERE
 book_id = b.id
 ) score
 FROM
 book_book b
 LEFT JOIN book_orders o ON b.id = o.bid
GROUP BY
 b.id

Query results

score is three consumption numbers, separated by commas

Performance Analysis

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Example of how to retrieve the latest data using MySQL multi-table association one-to-many query
  • A brief discussion on the implementation of multi-table unrelated query in MySQL
  • MySQL detailed explanation of multi-table association query

<<:  Let’s talk in detail about how JavaScript affects DOM tree construction

>>:  Diagram of the process of implementing direction proxy through nginx

Recommend

How to change the domestic image source for Docker

Configure the accelerator for the Docker daemon S...

Summary of various postures of MySQL privilege escalation

Table of contents 1. Write Webshell into outfile ...

Implementation of CSS border length control function

In the past, when I needed the border length to b...

How to create scheduled tasks using crond tool in Linux

Preface Crond is a scheduled execution tool under...

Detailed explanation of the text-fill-color property in CSS3

What does text-fill-color mean? Just from the lit...

TypeScript generic parameter default types and new strict compilation option

Table of contents Overview Create a type definiti...

HTML table tag tutorial (13): internal border style attributes RULES

RULES can be used to control the style of the int...

Use nginx to configure domain name-based virtual hosts

1. What is a virtual host? Virtual hosts use spec...

Sample code for installing ElasticSearch and Kibana under Docker

1. Introduction Elasticsearch is very popular now...

Use image to submit the form instead of using button to submit the form

Copy code The code is as follows: <form method...

JavaScript implementation of magnifying glass details

Table of contents 1. Rendering 2. Implementation ...