Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the functions of concat, concat_ws and group_concat in MySQL

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces MySQL row transfer, column transfer, connection string concat, concat_ws, group_concat function how to use, the article introduces in great detail, has a certain reference value, interested friends must read!

How to use it:

CONCAT (str1,str2, …)

Returns a string that results in a connection parameter. If any parameter is NULL, the return value is NULL.

Note:

If all parameters are non-binary strings, the result is a non-binary string.

If the argument contains any binary string, the result is a binary string.

A numeric parameter is converted to its equivalent binary string format; to avoid this, use the explicit type cast, such as:

SELECT CONCAT (CAST (int_col AS CHAR), char_col)

The concat function of MySQL can concatenate one or more strings, such as

Mysql > select concat ('10')

+-+

| | concat ('10') | |

+-+

| | 10 |

+-+

1 row in set (0.00 sec)

Mysql > select concat ('11, 22, 22, 33)

+-- +

| | concat ('11, 22, 33) | |

+-- +

| | 112233 |

+-- +

1 row in set (0.00 sec)

When concatenating a string, the concat function of MySQL returns NULL as long as one of them is NULL

Mysql > select concat ('11, 22, 13, 22, 9, 9, 9, 9, 22, 9, 9, 22, 9, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 9, 9, 6, 6, 6, 6, 6, 6, 9, 6, 6, 6, 6, 6, 6, 9, 9, 6, 6, 6, 6, 6, 6, 6, 6, 9, 9, 9, 9, 9, 9, 9, 9, 6, 6, 9, 9, 6, 9, 9, 6, 6, 6, 9, 6, 6, 6, 9, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6

+-- +

| | concat ('11 years, 22 minutes, 22 months) | |

+-- +

| | NULL |

+-- +

1 row in set (0.00 sec)

Concat_ws function in MySQL

How to use it:

CONCAT_WS (separator,str1,str2,...)

CONCAT_WS () stands for CONCAT With Separator and is a special form of CONCAT (). The first parameter is the delimiter of the other parameters. The position of the delimiter is placed between the two strings to concatenate. The delimiter can be a string or other parameters.

Note:

If the delimiter is NULL, the result is NULL. Function ignores the NULL value after any delimiter arguments.

If separated by commas after connection

Mysql > select concat_ws (',','11, 22, 22, 33)

+-+

| | concat_ws (',','11), 22 (33) | |

+-+

| | 11pr 22 pr 33 | |

+-+

1 row in set (0.00 sec)

Unlike the concat function in MySQL, the concat_ws function does not return NULL because of the NULL value when it is executed

Mysql > select concat_ws (',','11,'11,'11,'11,'11,','11,','11,','11,','11,','11,','11,','11,','11,','11,','11,','11,','11,','11,','11,','11,','11,','11,','11,','11,','11,')

+-+

| | concat_ws (',','11 minutes, 22 minutes, null) | |

+-+

| | 11pr 22 |

+-+

1 row in set (0.00 sec)

Group_concat function in MySQL

The complete syntax is as follows:

Group_concat ([DISTINCT] the field to be connected [Order BY ASC/DESC sort field] [Separator 'delimiter'])

Basic query

Mysql > select * from aa

+-+ +

| | id | name |

+-+ +

| | 1 | 10 |

| | 1 | 20 |

| | 1 | 20 |

| | 2 | 20 |

| | 3 | 200 | |

| | 3 | 500 |

+-+ +

6 rows in set (0.00 sec)

Group by id, print the values of the name field on a line, separated by commas (default)

Mysql > select id,group_concat (name) from aa group by id

+-+ +

| | id | group_concat (name) |

+-+ +

| | 1 | 10, 20, 20, 20 |

| | 2 | 20 |

| | 3 | 200500 | |

+-+ +

3 rows in set (0.00 sec)

Group the values in id and print the values of the name field on a line separated by semicolons

Mysql > select id,group_concat (name separator';') from aa group by id

+-- +

| | id | group_concat (name separator';') |

+-- +

| | 1 | 10: 20 | 20 |

| | 2 | 20 |

| | 3 | 200 × 500 |

+-- +

3 rows in set (0.00 sec)

Group by id and print the values of the de-redundant name fields on one line

Comma separation

Mysql > select id,group_concat (distinct name) from aa group by id

+-- +

| | id | group_concat (distinct name) |

+-- +

| | 1 | 100.20 |

| | 2 | 20 |

| | 3 | 200500 | |

+-- +

3 rows in set (0.00 sec)

Group the values in id, print the values of the name field on one line, separated by commas, and sort them in reverse order in name

Mysql > select id,group_concat (name order by name desc) from aa group by id

+-- +

| | id | group_concat (name order by name desc) |

+-- +

| | 1 | 20, 20, 10 |

| | 2 | 20 |

| | 3 | 500200 | |

+-- +

3 rows in set (0.00 sec)

Repeat () function

Used to copy a string, such as' ab' 'for the string to be copied, and 2 for the number of copies

Mysql > select repeat ('ab',2)

+-+

| | repeat ('ab',2) | |

+-+

| | abab |

+-+

1 row in set (0.00 sec)

Also such as

Mysql > select repeat ('axioma2)

+-+

| | repeat ('astatine 2) | |

+-+

| | aa |

+-+

1 row in set (0.00 sec)

The above is all the contents of this article entitled "column conversion, column conversion, connection string concat, concat_ws, group_concat function in MySQL". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report