Detailed explanation of concat correlation functions in mysql
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!