Create GroupGrouping is established in the GROUP BY clause of the SELECT statement. example: SELECT vend_id, COUNT(*) AS num_prods FROM products GROUP BY vend_id; GROUP BY
Filter GroupsIf one wants to list all orthopedics departments that have at least two orders, the data must be filtered based on the complete grouping rather than individual rows. You can use HAVING SELECT cust_id, COUNT(*) AS orders FROM orders GROUP BY cust_id HAVING COUNT(*) >= 2; The difference between HAVING and WHERE is that WHERE filters before data grouping, while HAVING filters after grouping. Of course, both can appear in the same statement. List suppliers with 2 or more products with a price of 10 or more SELECT vend_id, COUNT(*) AS num_prods FROM products WHERE prod_price >= 10 GROUP BY vend_id HAVING COUNT(*) >= 2; List suppliers with two top products SELECT vend_id, COUNT(*) AS num_prods FROM products GROUP BY vend_id HAVING COUNT(*) >= 2; Grouping and Sorting
Retrieve the order number and total order price of orders with a total order price greater than or equal to 50 SELECT order_num, SUM(quantity*item) AS ordertotal FROM orderitems GROUP BY order_num HAVING SUM(quantity*item_price) >= 50; If you sort the output by total order price SELECT order_num, SUM(quantity * item_price) AS ordertotal FROM orderitems GROUP BY order_num HAVING SUM(quantity * item_price) >= 50 ORDER BY ordertotal; Order of SELECT clauses
The above is a detailed explanation of MySQL data grouping. For more information about MySQL data grouping, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Analysis of the usage of Xmeter API interface testing tool
>>: Markup Language - Image Replacement
This article shares the specific code for impleme...
Preface: Vue3 has been released for a long time. ...
the term: 1. VM: Virtual Machine step: 1. Downloa...
1. Framework A browser document window can only d...
Table of contents 1. The difference between trans...
Table of contents 1. Get the first link first 2. ...
If the developer uses Dockerfile to build the ima...
background Getting the slow query log from mysql....
FFMPEG 3.4.1 version parameter details Usage: ffm...
This article example shares the specific code for...
question The tomcat container was successfully ad...
Click here to return to the 123WORDPRESS.COM HTML ...
Two parameters of Mysql paging select * from user...
Use canvas to write a colorful clock! 1. Title (1...
Table of contents Introduction What does an itera...