Analysis of the combined use of aggregate functions and connection tables _ MySQL

Source: Internet
Author: User
Analyze the combination of aggregate functions and connection tables using bitsCN.com

Analyze the combined use of aggregate functions and connection tables

Look at this relationship

MERS (cust_id, cust_name, cust_contact)

Orders (order_num, cust_id, order_date)

If there is such a search statement:

SQL code

Select cust_name, cust_contact ,(

Select count (*) from Orders

Where Orders. cust_id = Customers. cust_id

Group by cust_id)

From MERS

The procedure is as follows:

1. retrieve the order quantity of each cust_id from the Orders table

This can be done because the isolated count (*) is aggregated based on the group by customer number.

In addition, the join table operation is performed before aggregation. Connect orders and MERS.

In this way, only the customer information is agreed.

2. the outer layer lists other required customer information.

Note:

The inner layer is usually used as an isolated aggregate function,

Before grouping operations on the inner layer, the outer layer must be connected.

The selection columns behind the outer select are basically in the outer table.

To sum up, is this sentence a subquery or a normal connection.

Obviously. It is a subquery that contains a select sentence in the select statement.

Very special. clauses can be used as a search column !~ That's it.

In the future, we will try to group but involve two different tables.

BitsCN.com

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.