A brief analysis of the usage of USING and HAVING in MySQL

A brief analysis of the usage of USING and HAVING in MySQL

This article uses examples to illustrate the usage of USING and HAVING in MySQL. Share with you for your reference, the details are as follows:

USING

Used to give join conditions when joining tables (which can be understood as abbreviated form), such as

SELECT * FROM table1
JOIN table2 ON table1.id = table2.id

Using USING can be written as

SELECT * FROM table1
JOIN table2 USING (id)

HAVING

HAVING was introduced because WHERE cannot be used with statistical functions.

For example, the order table has the following fields:

id , date , price , customer

To find customers whose order total is less than 2000, you can write:

SELECT customer, SUM(price) FROM order
GROUP BY customer
HAVING SUM(price)<2000

Find the total order amount of orders exceeding 1500 for a specified customer:

SELECT customer,SUM(price) FROM order
WHERE customer = '…' OR customer = '…'
GROUP BY customer
HAVING SUM(price) > 1500

Readers who are interested in more MySQL-related content can check out the following topics: "Summary of MySQL Common Functions", "Summary of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Summary of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock-Related Skills".

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

You may also be interested in:
  • A brief discussion on the usage of using in MySQL database
  • Detailed explanation of Mysql using usage examples

<<:  Solve the problem of case sensitivity of Linux+Apache server URL

>>:  Solution to 404 error when downloading apk file from IIS server

Recommend

The pitfall record of the rubber rebound effect of iOS WeChat H5 page

Business requirements One of the projects I have ...

MySQL 8.0.18 uses clone plugin to rebuild MGR implementation

Assume that a node in the three-node MGR is abnor...

Detailed explanation of the payment function code of the Vue project

1. Alipay method: Alipay method: Click Alipay to ...

Summary of Linux sftp command usage

sftp is the abbreviation of Secure File Transfer ...

In-depth explanation of MySQL learning engine, explain and permissions

engine Introduction Innodb engine The Innodb engi...

Button is stretched on both sides in IE

When you write buttons (input, button), you will f...

Detailed explanation of JavaScript's built-in objects Math and strings

Table of contents Math Objects Common properties ...

React method of displaying data in pages

Table of contents Parent component listBox List c...

Tutorial on how to install htop on CentOS 8

If you are looking to monitor your system interac...

Let IE support CSS3 Media Query to achieve responsive web design

Today's screen resolutions range from as smal...

MySQL Optimization: Cache Optimization (Continued)

There are caches everywhere inside MySQL. When I ...

OpenSSL implements two-way authentication tutorial (with server and client code)

1. Background 1.1 Problems A recent product testi...

Solution to the problem of invalid line-height setting in CSS

About the invalid line-height setting in CSS Let&...

CentOS7 upgrade kernel kernel5.0 version

Upgrade process: Original system: CentOS7.3 [root...