Get the App
SLTechnology News&Howtos  ›  Database  › 

Handouts for sorting different strings in Mysql database

Shulou Source: shulou.com Published: 2022-06-01 20:13:18 10月02日 Update

This article mainly introduces the Mysql database different string sorting handout, the article content is the author carefully selected and edited, Mysql database different string sorting handout has a certain pertinence, for everyone's reference significance or greater, the following with the author to understand the next topic content.

Next, I sort it in server_id from the database. Let's take a look at the sorted results:

Select server_id from cardserver where game_id = 1 order by server_id desc limit 10

+-+

| | server_id |

+-+

| | 8 |

| | 7 |

| | 6 |

| | 5 |

| | 4 |

| | 3 |

| | 2 |

| | 10 |

| | 1 |

+-+

Obviously, the result I want should be 10, 8, 7, 6, 5 and so on. But this 10 comes after 2. Arranged according to the string. Actually, I want to arrange it as a numerical value.

Manually convert the type:

Use the following method to sort it after server_id+0, and the problem is solved.

Select server_id from cardserver where game_id = 1 order by server_id+0 desc limit 10

+-+

| | server_id |

+-+

| | 10 |

| | 8 |

| | 7 |

| | 6 |

| | 5 |

| | 4 |

| | 3 |

| | 2 |

| | 1 |

+-+

Use the MySQL function CAST/CONVERT:

Mysql provides us with two type conversion functions: CAST and CONVERT, how can we let go of something ready-made?

The CAST () and CONVERT () functions can be used to get a value of one type and produce a value of another type.

This type can be one of the following values:

BINARY [(N)]

CHAR [(N)]

DATE

DATETIME

DECIMAL

SIGNED [INTEGER]

TIME

UNSIGNED [INTEGER]

So we can also use CAST to solve the problem:

Select server_id from cardserver where game_id = 1 order by CAST (server_id as SIGNED) desc limit 10

You can also use CONVERT to fix this problem:

Select server_id from cardserver where game_id = 1 order by CONVERT (server_id,SIGNED) desc limit 10

After reading the above handouts on the sorting of different strings in Mysql database, many readers must have some understanding. If you need more industry knowledge and information, you can continue to follow our industry information column.

Tags: Sort type character string data database different handout function question content author result industry obvious thing two topic information meaning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple macOS Xiaomi Shulou Information vpn