“This is the 16th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

String function

Mysql not only provides operations for auxiliary record aggregation, but also provides common character operations such as concatenation, interception and replacement of strings

concat

Concat () takes one or more string arguments and eventually concatenates them together to return a single string, but if any of the arguments is null the entire string is returned

In our example, if we need to insert a delimiter between multiple characters, we need to pass the delimiter as a normal character. Mysql provides us with an enhanced character concat_ws function

concat_ws

concat_ws(seperator,string1,string2, … ) This function will automatically insert the specified delimiter between multiple arguments, starting with the delimiter (required) and the string we want to concatenate

Here’s another example of how concat_ws handles null character data

As shown, concat_WS does not return the entire result as NULL if there is empty data in the concatenated character, but simply ignores the empty character

length

Get the length of the string in bytes. In mysql, different character encodings take up different numbers of bytes. For example, utf8mb4, the most commonly used, takes up 3 bytes, and utf8mb4, the most common in mysql8, takes up 4 bytes

We can use show character set; To view the character set currently supported by mysql and the number of bytes they require

Anyway, in mysql, Chinese characters encoded in UTF8 take up 3 bytes

char_length

Compared with length, char_length is counted by character, that is, char_length does not care about character encoding, but only counts the number of characters

For example, we have done a voluntary pay site, need to attract people to click but do not want to be a direct resource to crawl away, need to do the content of the article length intercept, this implementation now has two schemes, ① in the background to do; ② Directly in the database processing, background code does not need to move, with mybaits XML form OF SQL, changed the direct deployment, very simple