Detailed example of concatenating multiple fields in mysql

Detailed example of concatenating multiple fields in mysql

The MySQL query result row field splicing 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 there is NULL in the connection string, the return 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,...) stands for concat with separator and is a special form of concat(). The first parameter is the separator for the other parameters. The delimiter is placed between the two strings to be concatenated. The separator can be a string or other parameters.

mysql> select concat_ws(':','1','2','3') from test;
+----------------------------+
| concat_ws(':','1','2','3') |
+----------------------------+
| 1:2:3 |
+----------------------------+

If the delimiter is NULL, the return value is NULL:

mysql> select concat_ws(NULL,'1','2','3') from test; 
+-----------------------------+
| concat_ws(NULL,'1','2','3') |
+-----------------------------+
| NULL | 
+-----------------------------+

If there is NULL 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 |
+-------------------------------------------+

You can judge 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 | 
+---------------------------------------------+

Supplement: Supplement: When grouping in MySQL, concatenate the values ​​of a field

t_dog table

t_vaccine table

t_dog_vaccine table

Problem Description

I need to query the vaccine corresponding to each dog in dog_vaccine. Since there is a many-to-many relationship between dogs and vaccines, one dog may correspond to multiple vaccines, but I want to concatenate these multiple vaccines into one using strings and then map them to a Java entity class.

Solve it step by step

I used two left join queries to associate the three tables t_dog, t_vaccine, and t_dog_vaccine.

select dv.id as id,
 d.dog_name as dogName,v.vaccine_name 
 from 
 t_dog_vaccine dv
  left join t_dog d on dv.dog_id = d.id
  left join t_vaccine v on dv.vaccine_id = v.id

Although this matches the dog and the vaccine, there are multiple records for each dog, and I only want data such as the dog's name and the values ​​of multiple vaccine_names merged into a string. After thinking about it, I suddenly thought of group by.

select dv.id as id,
 d.dog_name as dogName,v.vaccine_name 
 from 
 t_dog_vaccine dv
  left join t_dog d on dv.dog_id = d.id
  left join t_vaccine v on dv.vaccine_id = v.id 
  group by dogName

Using group by is obviously not the result I expected. Although there is only one dogName, there is only one vaccine corresponding to it. I want multiple vaccine names, what should I do? Is there a function that can do string concatenation? Emmmmm, got it, group_concat. . .

select dv.id as id,
 d.dog_name as dogName,
 group_concat(v.vaccine_name) as dogVaccineName  
 from 
 t_dog_vaccine dv
  left join t_dog d on dv.dog_id = d.id
  left join t_vaccine v on dv.vaccine_id = v.id 
 group by d.dog_name

This has achieved the effect I wanted.

group_concat usage

Default usage

select group_concat(vaccine_name) as dogVaccineName from t_vaccine where id in(select vaccine_id from t_dog_vaccine where dog_id = 1)

The default delimiter for group_concat is ','. If we want to change this delimiter, we can do so.

REPLACE(group_concat(vaccine_name),',','Fill in the separator you want to change here')

For example, I want to change the default one to;

select REPLACE(group_concat(vaccine_name),',',';') as dogVaccineName from t_vaccine where id in(select vaccine_id from t_dog_vaccine where dog_id = 1)

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • How to assign default values ​​to fields when querying MySQL
  • Add a field to the table in the MySQL command line (field name, whether it is empty, default value)
  • MySQL table field setting default value (graphic tutorial and pay attention to details)
  • In-depth explanation of hidden fields, a new feature of MySQL 8.0
  • A brief discussion on which fields in Mysql are suitable for indexing
  • mysql update case update field value is not fixed operation
  • When modifying a record in MySQL, the update operation field = field + string
  • How to split and merge multiple values ​​in a single field in MySQL
  • Detailed analysis of the syntax of Mysql update to modify multiple fields and
  • How to set the default value of a MySQL field

<<:  Web page HTML code explanation: ordered list and unordered list

>>:  css Get all elements starting from the nth one

Recommend

Detailed explanation of Linux text processing tools

1. Count the number of users whose default shell ...

Vue implements multiple selections in the bottom pop-up window

This article example shares the specific code of ...

Detailed process of using vmware to test PXE batch installation server

Table of contents 1. Preparation 1. Prepare the e...

RGB color table collection

RGB color table color English name RGB 16 colors ...

XHTML: Frame structure tag

Frame structure tag <frameset></frameset...

How to manage cached pages in Vue

Table of contents Problem 1: Destruction 1. How t...

9 great JavaScript framework scripts for drawing charts on the web

9 great JavaScript framework scripts for drawing ...

Vue encapsulates the public function method of exporting Excel data

vue+element UI encapsulates a public function to ...

Problems encountered in the execution order of AND and OR in SQL statements

question I encountered a problem when writing dat...

Simple Implementation of HTML to Create Personal Resume

Resume Code: XML/HTML CodeCopy content to clipboa...

An article to understand Linux disks and disk partitions

Preface All hardware devices in the Linux system ...

Vue mobile terminal determines the direction of finger sliding on the screen

The vue mobile terminal determines the direction ...

HTML left and right layout example code

CSS: Copy code The code is as follows: html,body{ ...

Solution to MySQL server login error ERROR 1820 (HY000)

Fault site: Log in to the MySQL server and get th...