mysql learning, convert strings into numbers and compare and sort

mysql learning, convert strings into numbers and compare and sort


SUBSTRING_INDEX ([column name], [separator], [number of segments])
column name: the column name to be divided into the content of the column
Separator: the symbol used to cut the number
of segments: the length taken after cutting

The following example illustrates the parameters:
table info
column c_code
value
1-10-ache
then
select SUBSTRING_INDEX(c_code,'-',1) as c_code from info
will output
c_code
1
and select SUBSTRING_INDEX(c_code,'-',2) as c_code from info
will output
1-10
select SUBSTRING_INDEX(c_code,'-',-1) as c_code from info
will output
ache
Here -1 is the same as string interception in high-level languages, the same negative number means that the calculation starts from the back

sort, then
1-10-ache
1-2-ache
2-11-ache
2-3-ache
2-5-ache

select * from info order by (SUBSTRING_INDEX(c_code,'-',1)+0),(SUBSTRING_INDEX(SUBSTRING_INDEX(c_code,'-',2),'-',-1)+0) asc

Export
1-2-ache
1-10-ache
2-3-ache
2-5-ache
2-11-ache

Use double interception, and then use the mysql feature (+0 will automatically convert to a number) to compare the size of the value

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326518604&siteId=291194637