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
In web development, since the checkbox is small an...
Unicode is a character encoding scheme developed ...
Linux installation MySQL notes 1. Before installi...
js execution Lexical analysis phase: includes thr...
Table of contents 1. Object literals 2. The new k...
For .net development, I am more familiar with Mic...
Table of contents frame First-class error reporti...
Table of contents What is the slow query log? How...
This article shares the specific code of jQuery t...
Table of contents 1. Find the mirror 2. Download ...
Object's hasOwnProperty() method returns a Bo...
This article mainly introduces the example of rea...
Implementation ideas: Use text-shadow in CSS to a...
Record the installation and configuration method ...
Table of contents Preface 1. JDBC timeout setting...