Mysql implements three functions for field splicing

Mysql implements three functions for field splicing

When exporting data to operations, it is inevitable to splice the fields. If MySQL can do it, a lot of code can be saved.

  • concat()
  • concat_ws()
  • group_concat()

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.

concat_ws() can solve the separator problem at one time, and all values ​​will not be NUll just because one value is NUll.

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, group_concat() can concatenate other fields in the grouped state into a string for query

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 concat_ws() method to nest one layer.

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:
  • Implementation steps of Mysql merge results and horizontal splicing fields
  • Detailed explanation of MySQL string concatenation function GROUP_CONCAT
  • Detailed example of concatenating multiple fields in mysql

<<:  JavaScript realizes the effect of mobile modal box

>>:  Analysis of the process of implementing Nginx+Tomcat cluster under Windwos

Recommend

Detailed explanation of the background-position percentage principle

When I was helping someone adjust the code today,...

How to solve the problem of automatic package update in Debian system

I don't know when it started, but every time ...

8 Reasons Why You Should Use Xfce Desktop Environment for Linux

For several reasons (including curiosity), I star...

CSS3 realizes the mask barrage function

Recently I saw a barrage effect on B station call...

JS Easy to understand Function and Constructor

Table of contents 1. Overview 1.1 Creating a func...

MySQL database SELECT query expression analysis

A large part of data management is searching, and...

Implementation of multi-environment configuration (.env) of vue project

Table of contents What is multi-environment confi...

JavaScript to achieve a simple page countdown

This article example shares the specific code of ...

JavaScript Basics: Immediate Execution Function

Table of contents Immediately execute function fo...

react-diagram serialization Json interpretation case analysis

The goal of this document is to explain the Json ...

Quickly solve the Chinese input method problem under Linux

Background: I'm working on asset reporting re...

How does MySQL achieve multi-version concurrency?

Table of contents MySQL multi-version concurrency...

Solution to Element-ui upload file upload restriction

question Adding the type of uploaded file in acce...