Difference between concat and concat_ws () and some practical string functions of MySQL _ MySQL
Source: Internet
Author: User
Difference between concat and concat_ws () and several practical MySQL string functions bitsCN. com1 and concat ()
1.1 The concat function of MySQL can connect one or more strings, such
Mysql> select concat ('10 ');
+ -------------- +
| Concat ('10') |
+ -------------- +
| 10 |
+ -------------- +
1 row in set (0.00 sec)
When Oracle concat functions are connected, NULL is not returned as long as a string is not NULL.
SQL> select concat ('11', NULL) from dual;
CONCAT
--
11
2. the concat_ws () function indicates concat with separator, which has a separator string connection.
For example, separate the strings with commas (,).
Mysql> select concat_ws (',', '11', '22', '33 ');
Unlike concat, the concat_ws function does not return NULL because of the NULL value during execution.
Mysql> select concat_ws (',', '11', '22', NULL );
+ ------------------------------- +
| Concat_ws (',', '11', '22', NULL) |
+ ------------------------------- +
| 11,22 |
+ ------------------------------- +
1 row in set (0.00 sec)
3. group_concat () can be used for row-to-column Conversion. Oracle does not have such a function.
The complete syntax is as follows:
Group_concat ([DISTINCT] field to be connected [Order by asc/DESC sorting field] [Separator 'delimiter'])
Example
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)
3.1 Group by id and print the value of the name field in one row, separated by commas (, by default)
Mysql> select id, group_concat (name) from aa group by id;
+ ------ + -------------------- +
| Id | group_concat (name) |
+ ------ + -------------------- +
| 1 | 10, 20, 20 |
| 2 | 20 |
| 3 | 200,500 |
+ ------ + -------------------- +
3 rows in set (0.00 sec)
3.2 Group by id, print the value of name field in one row, 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)
3.3 Group by id and print the value of the redundant name field in one row, separated by commas
Mysql> select id, group_concat (distinct name) from aa group by id;
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.