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

Detailed steps for building a React application with a Rails API

Table of contents Backend: Rails API part Front-e...

Problems encountered by MySQL nested transactions

MySQL supports nested transactions, but not many ...

Cleverly use CSS3's webkit-box-reflect to achieve various dynamic effects

In an article a long time ago, I talked about the...

Sending emails in html is easy with Mailto

Recently, I added a click-to-send email function t...

Summary of the use of MySQL date and time functions

This article is based on MySQL 8.0 This article i...

Ten popular rules for interface design

<br />This is an article I collected a long ...

Detailed explanation of LVM seamless disk horizontal expansion based on Linux

environment name property CPU x5650 Memory 4G dis...

centos7.2 offline installation mysql5.7.18.tar.gz

Because of network isolation, MySQL cannot be ins...

How to use partitioning to optimize MySQL data processing for billions of data

When MySQL queries tens of millions of data, most...

Detailed explanation of MySql view trigger stored procedure

view: When a temporary table is used repeatedly, ...

How to solve the error of connecting to the database when ServerManager starts

Servermanager startup connection database error R...