The string truncation function of MySQL is a powerful function in MySQL. The following describes the usage of the string truncation function of MySQL in detail, hoping to help you.
When creating the view today, it is very convenient to use string Truncation in MySQL.
I feel that the string function of MySQL intercepts characters, which is more powerful than program interceptions such as PHP or JAVA. So I want to make a record here and hope it will be useful to everyone.
MySQL string truncation function:
1. Extract strings from the left
Left (str, length)
Note: left (truncated field, truncated length)
Example: select left (content, 200) as abstract from my_content_t
2. Extract strings from the right
Right (str, length)
Note: right (intercepted field, truncated length)
Example: select right (content, 200) as abstract from my_content_t
3. truncate a string
Substring (str, pos)
Substring (str, pos, length)
Description: substring (truncated field, starting from the nth digit)
Substring (truncated field, starting from the nth digit and intercepting the length)
Example: select substring (content, 5) as abstract from my_content_t
Select substring (content, 5,200) as abstract from my_content_t
Note: If the number of digits is negative, for example,-5, it is the length from the last digit to the end or truncation of the string)
4. truncate a string by keyword
Substring_index (str, delim, count)
Note: substring_index (intercepted field, keyword, number of times the keyword appears)
Example: select substring_index ("blog.chinabyte.com", ".", 2) as abstract from my_content_t
Result: blog. chinabyte
Note: If the number of times a keyword appears is negative, for example,-2, it is counted from the last to the end of the string)
Result: chinabyte.com
If the keyword cannot be found in the string, all contents of the string are returned.
MySQL string segmentation implementation
MySQL string addition function usage example
Mysql query case
How to query duplicate records in MYSQL
How to Implement MySQL full-text Query