Usage of row-to-column conversion and GROUP_CONCAT function in MySQL

Background: You have two tables, one is yjs_jingqingtable and the other is yjs_partiestable. yjs_jingqingThe table contains some police information, and yjs_partiesthe table contains information about multiple parties related to each police.

Purpose: You want to yjs_jingqingget the sum of each alarm and all parties related to each alarm from the table , jq_idand connect them into a string and return it as the result.pts_nameplate_no

In this SQL query, GROUP_CONCAT()this is achieved by using subqueries and functions. The following is an explanation of the query statement:

SELECT a.jq_id,
    (SELECT GROUP_CONCAT(pts_name) AS pts_name FROM yjs_parties b WHERE b.jq_id = a.jq_id) AS pts_name,
    (SELECT GROUP_CONCAT(plate_no) AS plate_no FROM yjs_parties b WHERE b.jq_id = a.jq_id) AS plate_no
FROM `yjs_jingqing` a

explain:

  1. The outer query yjs_jingqingselects jq_idfields from the table as part of the result.
  2. The first subquery (SELECT GROUP_CONCAT(pts_name) AS pts_name FROM yjs_parties b WHERE b.jq_id = a.jq_id)is used to get all the values ​​associated with each alert pts_nameand GROUP_CONCAT()concatenate them into a string using the function as a pts_nameresult of the alias.
  3. The second subquery (SELECT GROUP_CONCAT(plate_no) AS plate_no FROM yjs_parties b WHERE b.jq_id = a.jq_id)is used to get all the values ​​associated with each alert plate_noand GROUP_CONCAT()concatenate them into a string using the function as a plate_noresult of the alias.

The final result will contain, for each alert , all sum values jq_id​​associated with each alert .pts_nameplate_no

When using the GROUP_CONCAT function, we can concatenate the values ​​of a field in multiple rows into a string. This function is very useful when dealing with aggregation queries.

The syntax of the GROUP_CONCAT function is as follows:

GROUP_CONCAT([DISTINCT] expr [, expr ...]
             [ORDER BY {unsigned_integer | col_name | expr}
                 [ASC | DESC] [, col_name ...]]
             [SEPARATOR str_val])

The GROUP_CONCAT function takes one or more expressions as arguments and concatenates the values ​​of these expressions into a single string. Duplicate values ​​can be eliminated using the DISTINCT keyword. The results can be sorted using the ORDER BY clause. You can also set the separator for the connection string by specifying the SEPARATOR parameter.

In our query, we use the GROUP_CONCAT function to concatenate the pts_namesum of all parties associated with each alert plate_no.

For example, if there is a case with multiple parties, the GROUP_CONCAT function will concatenate their sum pts_nameinto plate_noa string, using commas as separators.

Let's look at an example, assuming the following data:

yjs_jingqing表:
jq_id
1
2

yjs_parties表:
jq_id  pts_name    plate_no
1      John Doe    ABC123
1      Jane Smith  DEF456
2      Tom Brown   GHI789
2      Alice Green JKL012

Using our query statement, we will get the following results:

jq_id   pts_name                  plate_no
1       John Doe,Jane Smith       ABC123,DEF456
2       Tom Brown,Alice Green     GHI789,JKL012

In this way, we can obtain the sum of each alarm and all parties related to each alarm in a query statement , jq_idand connect them into a string to return.pts_nameplate_no

Guess you like

Origin blog.csdn.net/weixin_65837469/article/details/131549894