Splicing a field after grouping in mysql - GROUP_CONCAT function

Splice a field after grouping in mysql

background

The dictionary table structure is as follows:

dictionary type dictionary key dictionary value
table data

Question: If the dictionaries are of the same type, concatenate the values ​​of the dictionaries together

Use of the GROUP BY() function

We can use java to group, although it will be a little troublesome, we can also get the results we want.
If we want to use this grouping result to join other tables, it will be difficult. At this time, we can use the GROUP BY() function to group the data, as follows:

SELECT
	line_type,
	dict_value 
FROM
	bas_business_type_setting 
GROUP BY line_type

Result:
result 1
Although it is divided into groups, there is only one value in the dictionary, which does not meet our requirements. How to splice this field?
mysql provides us with the GROUP_CONCAT() function

Use of the GROUP_CONCAT() function

SELECT
	line_type,
	GROUP_CONCAT(dict_value)
FROM
	bas_business_type_setting 
	GROUP BY line_type

Result:
result 2
The GROUP_CONCAT function can splice the grouped fields, so that we can get the result we want, and we can also use this result to link other tables later to achieve more complex requirements.

Guess you like

Origin blog.csdn.net/qq_42547733/article/details/128644458