Background: You have two tables, one is yjs_jingqing
table and the other is yjs_parties
table. yjs_jingqing
The table contains some police information, and yjs_parties
the table contains information about multiple parties related to each police.
Purpose: You want to yjs_jingqing
get the sum of each alarm and all parties related to each alarm from the table , jq_id
and connect them into a string and return it as the result.pts_name
plate_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:
- The outer query
yjs_jingqing
selectsjq_id
fields from the table as part of the result. - 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 alertpts_name
andGROUP_CONCAT()
concatenate them into a string using the function as apts_name
result of the alias. - 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 alertplate_no
andGROUP_CONCAT()
concatenate them into a string using the function as aplate_no
result of the alias.
The final result will contain, for each alert , all sum values jq_id
associated with each alert .pts_name
plate_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_name
sum 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_name
into plate_no
a 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_id
and connect them into a string to return.pts_name
plate_no