Concatenating multiple mysql fields _ MySQL

Source: Internet
Author: User
The following two functions can be used to concatenate the query result line fields of Mysql: 1concat function mysqlselectconcat (39; 139;, 39; 239;, 39; 339;) fromtest; + --------------------- + | concat (39; 139;, 39; 2 concatenate the query result line fields of Mysql, which can be implemented using the following two functions:

1. concat function

mysql> select concat('1','2','3') from test ;+---------------------+| concat('1','2','3') |+---------------------+| 123 |+---------------------+
If NULL exists in the connection string, the returned result is NULL:
mysql> select concat('1','2',NULL,'3') from test ;+--------------------------+| concat('1','2',NULL,'3') |+--------------------------+| NULL |+--------------------------+
2. concat_ws function

Concat (separator, str1, str2,...) represents concat with separator, which is a special form of concat. The first parameter is the delimiter of other parameters. The separator is placed between the two strings to be connected. The delimiter can be a string or another parameter.

mysql> select concat_ws(':','1','2','3') from test ;+----------------------------+| concat_ws(':','1','2','3') |+----------------------------+| 1:2:3 |+----------------------------+
If the delimiter is NULL, the returned result is NULL:
mysql> select concat_ws(NULL,'1','2','3') from test; +-----------------------------+| concat_ws(NULL,'1','2','3') |+-----------------------------+| NULL | +-----------------------------+
If NULL exists in the parameter, it will be ignored:
mysql> select concat_ws(':','1','2',NULL,NULL,NULL,'3') from test ;+-------------------------------------------+| concat_ws(':','1','2',NULL,NULL,NULL,'3') |+-------------------------------------------+| 1:2:3 |+-------------------------------------------+
NULL can be judged and replaced with other values.
mysql>  select concat_ws(':','1','2',ifNULL(NULL,'0'),'3') from bank limit 1;  +---------------------------------------------+| concat_ws(':','1','2',ifNULL(NULL,'0'),'3') |+---------------------------------------------+| 1:2:0:3                                     | +---------------------------------------------+

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.