Detailed explanation of concat related functions in MySQL

Detailed explanation of concat related functions in MySQL

1. concat() function

Function: Concatenate multiple strings into one string

Syntax: concat(str1,str2,…) The string can be either a data table field or a specified string.

The return result is a string generated by the connection parameters. If any parameter is null, the return value of the record is null.

2. concat_ws() function

Function: Like concat(), it concatenates multiple strings into one string, but you can specify the separator at one time (concat_ws is concat with separator)

Syntax: concat_ws(separator,str1,str2,…)

Note: The first parameter specifies the separator. It should be noted that the separator cannot be null. If it is null, all returned results will be null.

3. group_concat() function

Let’s look at this requirement first:

Where user_id is the user id, fee is the consumption amount, and time is the consumption timestamp.

In the consumption record table, you need to find out the details of all consumption amounts for each user.

Obviously, a single group by is not enough to meet this requirement:

select max(time),fee from consumption group by user_id

The relationship between the fields after group by is staggered. We can only filter a certain field, but cannot guarantee that they belong to the same record.

At this time, the group_concat() function is used:

select user_id,GROUP_CONCAT(time,':',fee) from consumption group by user_id

The following results are obtained:

By processing the results, we can easily display the desired effect on the page.

Summarize

The above is the concat related functions in MySQL introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • How to use the concat function in mysql
  • A brief discussion on the concat function in MySQL. How to add a string before or after a field in MySQL
  • Detailed explanation of the usage of the concat function in MySQL (connecting strings)
  • Summary of mysql group_concat() function usage
  • Analysis of traps in using the MySQL statistical function GROUP_CONCAT
  • CONCAT Function Usage Tutorial in MySQL
  • Mysql database uses concat function to execute SQL injection query
  • In-depth understanding of group_concat function in MySQL
  • How to use the Mysql GROUP_CONCAT() function

<<:  Install Ubuntu 18 without USB drive under Windows 10 using EasyUEFI

>>:  Records of using ssh commands on Windows 8

Recommend

13 JavaScript one-liners that will make you look like an expert

Table of contents 1. Get a random Boolean value (...

Detailed explanation of browser negotiation cache process based on nginx

This article mainly introduces the detailed proce...

Nginx try_files directive usage examples

Nginx's configuration syntax is flexible and ...

Detailed explanation of nginx anti-hotlink and anti-crawler configuration

Create a new configuration file (for example, go ...

MySQL decimal unsigned update negative numbers converted to 0

Today, when verifying the concurrency problem of ...

Graphical tutorial on installing CentOS 7.3 on VMWare

Illustrated CentOS 7.3 installation steps for you...

JavaScript implementation of magnifying glass details

Table of contents 1. Rendering 2. Implementation ...

What you need to know about creating MySQL indexes

Table of contents Preface: 1. Create index method...

Implementation example of react project from new creation to deployment

Start a new project This article mainly records t...

Vue+echart realizes double column chart

This article shares the specific code of vue+echa...

Use the Linux seq command to generate a sequence of numbers (recommended)

The Linux seq command can generate lists of numbe...

JavaScript to achieve uniform animation effect

This article example shares the specific code for...

Implementation of vite+vue3.0+ts+element-plus to quickly build a project

Table of contents vite function Use Environment B...

MySQL master-slave replication configuration process

Main library configuration 1. Configure mysql vim...