When exporting data to operations, it is inevitable to splice the fields. If MySQL can do it, a lot of code can be saved.
Mysql does have several functions that can concatenate fields. concat() Use empty strings to concatenate multiple fields into one field mysql> select concat(id, type) from mm_content limit 10; +------------------+ | concat(id, type) | +------------------+ | 100818image | | 100824image | | 100825video | | 100826video | | 100827video | | 100828video | | 100829video | | 100830video | | 100831video | | 100832video | +------------------+ 10 rows in set (0.00 sec) However, if any field value is NULL, the result is NULL. mysql> select concat(id, type, tags) from mm_content limit 10; +------------------------+ | concat(id, type, tags) | +------------------------+ | NULL | | NULL | | NULL | | NULL | | NULL | | NULL | | NULL | | NULL | | NULL | | NULL | +------------------------+ 10 rows in set (0.00 sec) concat_ws() If you want to use a delimiter to split the fields in the above method, you need to insert a string in the middle of each field, which is very troublesome. mysql> select concat_ws(' ', id, type, tags) from mm_content limit 10; +--------------------------------+ | concat_ws(' ', id, type, tags) | +--------------------------------+ | 100818 image | | 100824 image | | 100825 video | | 100826 video | | 100827 video | | 100828 video | | 100829 video | | 100830 video | | 100831 video | | 100832 video | +--------------------------------+ 10 rows in set (0.00 sec) group_concat() The last one is amazing. Under normal circumstances, a statement written like this will definitely result in an error. mysql> select id from test_user group by age; ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test_user.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by However, mysql> select group_concat(name) from test_user group by age; +--------------------+ | group_concat(name) | +--------------------+ | wen,ning | | wxnacy,win | +--------------------+ 2 rows in set (0.00 sec) By default, commas are used to separate the data, but we can also specify a delimiter. mysql> select group_concat(name separator ' ') from test_user group by age; +----------------------------------+ | group_concat(name separator ' ') | +----------------------------------+ | wen ning | | wxnacy win | +----------------------------------+ 2 rows in set (0.00 sec) Arrange the strings in a certain order mysql> select group_concat(name order by id desc separator ' ') from test_user group by age; +---------------------------------------------------+ | group_concat(name order by id desc separator ' ') | +---------------------------------------------------+ | ning wen | | win wxnacy | +---------------------------------------------------+ 2 rows in set (0.00 sec) If you want to concatenate multiple fields, the default is to use an empty string for concatenation. We can use mysql> select group_concat(concat_ws(',', id, name) separator ' ') from test_user group by age; +------------------------------------------------------+ | group_concat(concat_ws(',', id, name) separator ' ') | +------------------------------------------------------+ | 1,wen 2,ning | | 3,wxnacy 4,win | +------------------------------------------------------+ 2 rows in set (0.00 sec) The above is the details of the three functions that Mysql implements field splicing. For more information about MySQL string splicing, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: JavaScript realizes the effect of mobile modal box
>>: Analysis of the process of implementing Nginx+Tomcat cluster under Windwos
Add rules to the el-form form: Define rules in da...
Basic Use <!DOCTYPE html> <html lang=&qu...
This article explains the difference between arro...
JDK Installation I won't go into too much det...
Table of contents 1. Scenario Description 2. Solu...
Table of contents Preface 1. for loop 2. while lo...
Network Communication Overview When developing an...
The <abbr> and <acronym> tags represen...
Recently, I encountered the need to embed a player...
Table of contents Preface 1. Binary Tree 1.1. Tra...
Recently, when I was sorting out the details of d...
Table of contents Preface 1. Demand and Effect ne...
1. HTML Image <img> 1. The <img> tag ...
This article mainly introduces the example analys...
Written in front: Sometimes you may need to view ...