Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of concat correlation functions in mysql

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report