Brief analysis of usage examples of USING and HAVING in MySQL

  • 2021-12-11 09:21:54
  • OfStack

This article illustrates the usage of USING and HAVING in MySQL. Share it for your reference, as follows:

USING

When used for table joins, a join condition is given (which can be understood as a shorthand), 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 function 1

Table order (Order) has the following fields:

id, date, price, customer

To find customers with a total order of less than 2000, you can write this:


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

Find the total number of orders with orders exceeding 1500 in the specified customer:


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

More readers interested in MySQL can check out the topics on this site: "MySQL Common Functions Summary", "MySQL Log Operation Skills Collection", "MySQL Transaction Operation Skills Collection", "MySQL Stored Procedure Skills Collection" and "MySQL Database Lock Related Skills Collection"

I hope this article is helpful to everyone's MySQL database.


Related articles: