MySQL advanced query and usage of GroupBy set _ MySQL

Source: Internet
Author: User
Introduction to MySQL advanced query and GroupBy collection bitsCN.com

MySQL advanced query and use of Group By sets

In MySQL, you can obtain the join value of expression combinations.

You can use DISTINCT to delete duplicate values. If you want to sort multiple result values, use the order by clause.

01GROUP_CONCAT02mysql> SELECT student_name,03    ->     GROUP_CONCAT(test_score)04    ->     FROM student05    ->     GROUP BY student_name;06Or:07mysql> SELECT student_name,08    ->     GROUP_CONCAT(DISTINCT test_score09    ->               ORDER BY test_score DESC SEPARATOR ' ')10    ->     FROM student11    ->     GROUP BY student_name;

In MySQL, you can obtain the join value of expression combinations. You can use DISTINCT to delete duplicate values. If you want to sort multiple result values, use the order by clause. To sort in reverse ORDER, add the DESC (descending) keyword to the column name that you want to sort using the order by clause. The default order is ascending. you can use ASC to specify it. SEPARATOR follows the string value in the middle of the value of the inserted result. The default value is comma (','). You can delete all separators by specifying the SEPARATOR.

PS: it is a string that can be used to obtain a combination of each sub-value of the group by aggregated item in a statement.

2 WITH ROLLUP

The group by clause allows an additional row to be added to the simple output end WITH the ROLLUP modifier. These rows represent high-level (or high aggregation) simple operations. ROLLUP allows you to answer questions from the perspective of multi-layer analysis.

Alternatively, you can use ROLLUP, which provides a double-layer analysis with an inquiry. Add a modifier with rollup to the group by statement to generate another row of results for the query. this row shows the total value of all years:

1mysql> SELECT year, SUM(profit) FROM sales GROUP BY year WITH ROLLUP;2+------+-------------+3| year | SUM(profit) |4+------+-------------+5| 2000 |        4525 |6| 2001 |        3010 |7| NULL |        7535 |8+------+-------------+

The total high clustering rows are marked by the NULL value in the year column.

When multiple group by columns exist, ROLLUP produces more complex results. At this time, each time a "break" appears in any column except the last classification column (value change), the inquiry will generate a high clustering cumulative row.

For example, in the absence of ROLLUP, a yearly, country, and product-based sales table list may be as follows:

01mysql> SELECT year, country, product, SUM(profit)02    -> FROM sales03    -> GROUP BY year, country, product;04+------+---------+------------+-------------+05| year | country | product    | SUM(profit) |06+------+---------+------------+-------------+07| 2000 | Finland | Computer   |        1500 |08| 2000 | Finland | Phone      |         100 |09| 2000 | India   | Calculator |         150 |10| 2000 | India   | Computer   |        1200 |11| 2000 | USA     | Calculator |          75 |12| 2000 | USA     | Computer   |        1500 |13| 2001 | Finland | Phone      |          10 |14| 2001 | USA     | Calculator |          50 |15| 2001 | USA     | Computer   |        2700 |16| 2001 | USA     | TV         |         250 |17+------+---------+------------+-------------+

Indicates that the output result of the total value is only at the analysis level of the year/country/product. After ROLLUP is added, the query will generate some additional rows:

01mysql> SELECT year, country, product, SUM(profit)02    -> FROM sales03    -> GROUP BY year, country, product WITH ROLLUP;04+------+---------+------------+-------------+05| year | country | product    | SUM(profit) |06+------+---------+------------+-------------+07| 2000 | Finland | Computer   |        1500 |08| 2000 | Finland | Phone      |         100 |09| 2000 | Finland | NULL       |        1600 |10| 2000 | India   | Calculator |         150 |11| 2000 | India   | Computer   |        1200 |12| 2000 | India   | NULL       |        1350 |13| 2000 | USA     | Calculator |          75 |14| 2000 | USA     | Computer   |        1500 |15| 2000 | USA     | NULL       |        1575 |16| 2000 | NULL    | NULL       |        4525 |17| 2001 | Finland | Phone      |          10 |18| 2001 | Finland | NULL       |          10 |19| 2001 | USA     | Calculator |          50 |20| 2001 | USA     | Computer   |        2700 |21| 2001 | USA     | TV         |         250 |22| 2001 | USA     | NULL       |        3000 |23| 2001 | NULL    | NULL       |        3010 |24| NULL | NULL    | NULL       |        7535 |25+------+---------+------------+-------------+

When you use ROLLUP, you cannot use the order by clause to sort the results at the same time. In other words, ROLLUP and order by are mutually exclusive. However, you can still control the sorting. In MySQL, group by can sort the results, and you can use clear ASC and DESC keywords in the columns specified in the group by list to sort individual columns. (No matter how you sort the high-level total rows added by ROLLUP, they still appear after the rows they are calculated ).

LIMIT can be used to LIMIT the number of lines returned to the client. LIMIT is used after ROLLUP, so this LIMIT will cancel the row added by ROLLUP.

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.