Oracle's wm_concat() and mysql's group_concat() merge the same column into one row, and multiple columns are spliced into one row

1.1 oracle syntax wmsys.wm_concat():
SELECT
NAME,
wmsys.wm_concat(subject_1) subject_1
FROM
table_name
GROUP BY
NAME
1.2 oracle syntax LISTAGG() (note, this is the syntax provided by Oracle 11g and above):
LISTAGG(measure_expr [, 'delimiter']) WITHIN GROUP (order_by_clause) [OVER query_partition_clause]
parameter description
OVER uses the OVER clause to define the window for calculation.
measure_expr can be any expression. Null values ​​in measure columns are ignored.
delimiter specifies the string used to delimit the metric values. This clause is optional and defaults to NULL.
SELECT
NAME, listagg(subject_1, ',') within GROUP (ORDER BY NAME)
FROM table_name
GROUP BY NAME
2.1 oracle syntax wmsys.wm_concat():

Through wm_concat() in oracle, the splicing of multiple columns can be realized:

select NAME, wmsys.wm_concat(age || ‘|’ || address) msg
from TK_USER_INFO
group by NAME;

Notice:

1. In the above SQL, || '|' || means that two columns are separated by |, and '|' can be replaced by any separator

1.3 mysql语法group_concat:
SELECT
NAME,
GROUP_CONCAT(distinct subject_1 order by subject_1 asc separator ‘,’) subject_1
FROM
table_name
GROUP BY
NAME
2.2 mysql 语法CONCAT()

select name, CONCAT(age ,address) msg from tk_user_info;

Notice:

1. The result of CONCAT splicing does not have a separator

2. If any concatenated column on the right is null, then return null
2.3 mysql syntax CONCAT_WS()

select name, CONCAT_WS(‘,’,age ,address) msg from tk_user_info;

Notice:

1. This function can define a separator, CONCAT_WS() stands for CONCAT With Separator

2. The syntax is CONCAT_WS(separator,str1,str2,…), the first parameter is the separator, and the others are spliced ​​columns.
2.4 mysql syntax GROUP_CONCAT()

select name, GROUP_CONCAT(age,‘,’,address) msg from tk_user_info group by name;

Notice:

1. Its syntax is (like the above explanation of multi-line splicing, it can perform de-redundancy, sorting, separators - different splicing columns can have different separators):

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

2. This sql is used to splice the columns after grouping a certain column, so the SQL must be followed by group by

Guess you like

Origin blog.csdn.net/qq_34690003/article/details/131132528