MySQL multiple field stitching

Source: Internet
Author: User

MySQL Query results row field stitching, can be implemented with the following two functions:

1. Concat function

Mysql> Select Concat (' 1 ', ' 2 ', ' 3 ') from test; +---------------------+| Concat (' 1 ', ' 2 ', ' 3 ') |+---------------------+| 123 |+---------------------+
Assuming 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 a delimiter for the other parameters. The position of the delimiter is placed between the two strings to be concatenated. The delimiter can be a string, or it can be a different parameter.

Mysql> Select Concat_ws (': ', ' 1 ', ' 2 ', ' 3 ') from test; +----------------------------+| Concat_ws (': ', ' 1 ', ' 2 ', ' 3 ') |+----------------------------+| 1:2:3 |+----------------------------+
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 is present in the parameter, it is ignored:

Mysql> Select Concat_ws (': ', ' 1 ', ' 2 ', Null,null,null, ' 3 ') from test; +-------------------------------------------+ | Concat_ws (': ', ' 1 ', ' 2 ', Null,null,null, ' 3 ') |+-------------------------------------------+| 1:2:3 |+-------------------------------------------+

The ability to infer null and replace it 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 | +---------------------------------------------+




MySQL multiple field stitching

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.