concat() 、concat_ws()和group_concat()

concat() 、concat_ws()和group_concat()

1.1 Differences

concat, concat_ws and group_concat are all used for splicing in sql statements, but they are used in different ways. concat is for splicing row data; concat_ws, like concat(), connects multiple strings into one String, but the separator can be specified at one time; group_concat is a data splicing for columns, and group_concat automatically generates commas.

1.2 concat() function

Function : Concatenate multiple strings into one string. Concat is for concatenating row data.
Syntax : concat(str1, str2,…)
Note : The return result is the string generated by the connection parameters. If any parameter is null, the return value is null.

1.2.1 Create table sql

CREATE TABLE Employee(
id int,
name VARCHAR(20),
birthday datetime
)engine myisam charset utf8;

insert into Employee 
values(1,'李四', '2001-12-20'), (2,'张三', '2019-6-6'), (3,'王二', '1996-3-23'), (4,'皮卡丘', NULL);

**SELECT * FROM Employee**
image.png
**SELECT CONCAT(id, name, birthday) FROM Employee**
image.png

1.3 concat_ws() function

Function : Like concat(), concatenate multiple strings into one string, but you can specify the separator at once (concat_ws is concat with separator)
Syntax : concat_ws(separator, str1, str2, ...)
Note : the first one The parameter specifies the delimiter. It should be noted that the separator cannot be null. If it is null, the return result will be null.
sql and data are the same as above

**SELECT CONCAT_WS(',',id, name, birthday) FROM Employee**
image.png

**SELECT CONCAT_WS(NULL,id, name, birthday) FROM Employee**
image.png

1.4 group_concat() function

Function : Connect the values ​​in the same group generated by group by and return a string result (group_concat is a data splicing for columns, and group_concat automatically generates commas).
Syntax : group_concat([distinct] Field to be connected [order by sorting field asc/desc] [separator 'separator'])
Description : Duplicate values ​​can be excluded by using distinct; if you want to sort the values ​​in the result, you can use order by clause; separator is a string value, defaulting to a comma.

1.4.1 Create table sql

create TABLE GroupConcatTest(
	id int,
	score int
)engine myisam charset utf8;

insert into GroupConcatTest 
values(1,10),(1,20),(1,20),(2,40),(3,20),(3,40),(3,50),(4, NULL);

**SELECT * FROM GroupConcatTest**
image.png

  • Requirement 1: Each ID is a row, and each row displays all the scores of the ID
SELECT
	id,
	GROUP_CONCAT( score ) 
FROM
	GroupConcatTest 
GROUP BY
	id

image.png

  • Requirement 2: Each id is a row and each row displays all the unique scores of the id.
SELECT
	id,
	GROUP_CONCAT( DISTINCT score ) 
FROM
	GroupConcatTest 
GROUP BY
	id

image.png

  • Requirement 3: Each id is a row. Each row displays all unique scores of the id and is sorted from high to low.
SELECT
	id,
	GROUP_CONCAT( DISTINCT score ORDER BY score DESC ) 
FROM
	GroupConcatTest 
GROUP BY
	id

image.png

  • Requirement 4: Each id is a row. Each row displays all the unique scores of the id, sorted from high to low, and separated by ';'
SELECT
	id,
	GROUP_CONCAT( DISTINCT score ORDER BY score DESC SEPARATOR ';' ) 
FROM
	GroupConcatTest 
GROUP BY
	id

image.png

reference:

  1. https://blog.csdn.net/qq_35531549/article/details/90383022?ops_request_misc=&request_id=&biz_id=102&utm_term=sql%20group_concat%E5%87%BD%E6%95%B0%E7%94%A8%E6%B3%95&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-2-90383022.142v59pc_rank_34_1,201v3add_ask&spm=1018.2226.3001.4187

Guess you like

Origin blog.csdn.net/hansome_hong/article/details/127432860