MySQL string connection function CONCAT, concat_ws,group_contact

Source: Internet
Author: User

This article is reproduced from De.cel, "MySQL string connection function CONCAT, Concat_ws,group_concat"

In search of MySQL How to implement a column of multiple rows of data merge into a row, found the group_contact function, which is more convenient than SQL Server in the Select @[email protected]+ column name from the table name, the form of a lot easier , a small summary of the string join function here.

CONCAT (str1, str2,...): Returns the string resulting from the connection parameter. If any one of the arguments is NULL, the return value is null.

Concat_ws (separator,str1,str2,...): CONCAT with separator, is a special form of CONCAT (). The first parameter is the delimiter for the other parameter. The position of the delimiter is placed between the two strings to be concatenated. The delimiter can be a string, or it can be another parameter. If the delimiter is null, the result is null. The function ignores NULL values after any delimiter parameters. e.g:

MySQL > SELECT concat_ws (', ', ' First name ', NULL, ' last name '); -First Name,last Name

Group_concat ([DISTINCT] field to connect [order by Asc/desc sort field] [Separator ' delimiter ']): Can be used to row to column. e.g: 
mysql> select * from TB;
 +------+------+ 
| id | name | &NBSP,
+------+------+ 
| 1 |-| &NBSP
| 1 | | &NBSP
| 1 | | &NBSP
| 2 | | &NBSP
| 3 | |  
| 3 | | &NBSP
+------+------+ 

myql> Select Id,group_concat (name) from the TB group by ID;

+------+--------------------+ 
| id | group_concat (name) | 
+------+--------------------+ | 1 | 10,20,20 | &NBSP
| 2 |           |  
| 3 | 200,500 | &NBSP
+------+--------------------+ 

Mysql> Select id< Span style= "color: #ff0000; Font-size:large; " >,group_concat (name separator '; ')  from TB Group by ID;

+------+----------------------------------+&NBSP;
| id | group_concat ( Name separator '; ') &NBSP;
| +------+----------------------------------+&NBSP;
| 1 | 10;20;20 | &NBSP
| 2 |        |  
| 3 | 200;500 | &NBSP
+------+----------------------------------+&NBSP;

MySQL > select Id,group_concat (distinct name)  from TB Group by ID;

+------+-----------------------------+
| ID | Group_concat (distinct name) |
+------+-----------------------------+
| 1 | 10,20 |
| 2 | 20 |
| 3 | 200,500 |
+------+-----------------------------+

MySQL string connection function CONCAT, concat_ws,group_contact

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.