Home >Daily Programming >Mysql Knowledge >What are the statements used for grouping in mysql?
MySQL grouping statement MySQL provides the following statements for grouping data: 1. GROUP BY: Group rows by grouping key; 2. HAVING: Filter grouping results; 3. WITH ROLLUP: Create summary rows; 4. WITH CUBE: Create multidimensional summary rows.
Group Statements in MySQL
The following statements are available in MySQL to group data:
GROUP BY
The GROUP BY statement groups together rows with the same grouping key value. The grouping key can be a single column or a combination of multiple columns.
<code class="sql">SELECT column_list FROM table_name GROUP BY grouping_column_list;</code>
HAVING
The HAVING statement is used to filter grouped results. It is used with the GROUP BY statement to apply conditions on grouped data sets.
<code class="sql">SELECT column_list FROM table_name GROUP BY grouping_column_list HAVING condition;</code>
WITH ROLLUP
WITH ROLLUP statement is used to create summary rows in a GROUP BY operation. It adds summary rows for each grouping level to the result set.
<code class="sql">SELECT column_list FROM table_name GROUP BY grouping_column_list WITH ROLLUP;</code>
WITH CUBE
The WITH CUBE statement is used to create multidimensional summary rows in a GROUP BY operation. It adds summary rows for all possible subset groupings to the result set.
<code class="sql">SELECT column_list FROM table_name GROUP BY grouping_column_list WITH CUBE;</code>
The above is the detailed content of What are the statements used for grouping in mysql?. For more information, please follow other related articles on the PHP Chinese website!