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

HTML Tutorial: HTML horizontal line segment

<br />This tag can display a horizontal line...

Linux yum package management method

Introduction yum (Yellow dog Updater, Modified) i...

Manually implement the two-way data binding principle of Vue2.0

In one sentence: Data hijacking (Object.definePro...

Detailed introduction to deploying k8s cluster on centos7 system

Table of contents 1 Version and planning 1.1 Vers...

How to obtain a permanent free SSL certificate from Let's Encrypt in Docker

1. Cause The official cerbot is too annoying. It ...

React's context and props explained

Table of contents 1. context 1. Usage scenarios 2...

Detailed installation and uninstallation tutorial for MySQL 8.0.12

1. Installation steps for MySQL 8.0.12 version. 1...

Understand the implementation of Nginx location matching in one article

Since the team is separating the front-end and ba...

Implementation code for infinite scrolling with n container elements

Scenario How to correctly render lists up to 1000...

Manually install mysql5.7.10 on Ubuntu

This tutorial shares the process of manually inst...

JavaScript Array Methods - Systematic Summary and Detailed Explanation

Table of contents Common array methods Adding and...

Steps to install cuda10.1 on Ubuntu 20.04 (graphic tutorial)

Pre-installation preparation The main purpose of ...

Solution to the failure of entering the container due to full docker space

Since the problem occurred rather suddenly and th...

Detailed explanation of how to use the canvas operation plugin fabric.js

Fabric.js is a very useful canvas operation plug-...