Generate the test table and populate the data according to the following statement
CREATE TABLE Z (a int, b int); INSERT into Z select 1,200;insert to Z select 1,100;insert into z select 1,100;insert INT O Z Select 2,400;insert into z select 2,500;insert to Z select 3,null;
Next, group By column A, and use the aggregate function Group_concat,sql statement as follows
SELECT A, Group_concat (b) from Zgroup by A;
The results of the operation are as follows in addition, the Group--concat aggregation function has some other uses, with the following syntax:
Group_concat ([DISTINCT] expr [, expr ...] [ORDER by {unsigned_integer | col_name | expr}[asc | DESC] [, col_name ...] [SEPARATOR Str_val])
The DISTINCT option removes duplicate values, the order selection can sort the columns, and the separator is used to select the stitching string. For example, in the example above, the column B can be de-weighed and sorted in descending order, with ":" Stitching the string. Its SQL statement can be written as:
SELECT A,group_concat (DISTINCT b ORDER by B DESC SEPARATOR ': ') from Zgroup to A;
Running results such as
MySQL's group_concat function