Get the App
SLTechnology News&Howtos  ›  Database  › 

Detailed explanation of concat correlation functions in mysql

Shulou Source: shulou.com Published: 2022-06-01 03:14:14 10月28日 Update

1. Concat () function

Function: concatenate multiple strings into a single string

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

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

Second, concat_ws () function

Function: like concat (), concatenate multiple strings into a single string, but you can specify the delimiter (concat_ws is concat with separator) at once.

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

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

Third, group_concat () function

Let's first take a look at this demand:

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

In the consumption consumption record table, you need to find out the details of the amount spent by each user.

Obviously, a single group by is no longer sufficient to achieve this requirement:

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

The relationship between the fields after group by is the wrong layer, we can only filter a single field, but can not guarantee that they belong to the same record.

This is the time to use the group_concat () function:

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

The results are as follows:

Data processing of the results makes it easy to show the effect we want on the page.

Summary

The above is the concat-related functions in mysql introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave a message for me, and the editor will reply you in time. Thank you very much for your support to the website!

Tags: Characters strings functions results consumption delimiters parameters fields functions multiple data users syntax amount requirements details one-time between that is effect Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Microsoft Xiaomi vpn Linux