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

mysql 8.0.15 winx64 decompression version graphic installation tutorial

Every time after installing the system, I have to...

MySQL randomly extracts a certain number of records

In the past, I used to directly order by rand() t...

Docker builds Redis5.0 and mounts data

Table of contents 1. Simple mounting of persisten...

How to install Jenkins using Docker

Table of contents 1. Pull the image 2. Create a l...

How to use linux commands to convert and splice audio formats

Install FFmpeg flac eric@ray:~$ sudo apt install ...

Detailed explanation of Vue's monitoring method case

Monitoring method in Vue watch Notice Name: You s...

Docker port mapping and external inaccessibility issues

The Docker container provides services and listen...

Detailed steps to configure my.ini for mysql5.7 and above

There is no data directory, my-default.ini and my...

vue+tp5 realizes simple login function

This article example shares the specific code of ...

HTML table cross-row and cross-column operations (rowspan, colspan)

Generally, the colspan attribute of the <td>...

Linux bridge method steps to bridge two VirtualBox virtual networks

This article originated from my complaints about ...

Linux installation MongoDB startup and common problem solving

MongoDB installation process and problem records ...