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

Handouts for sorting different strings in Mysql database

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

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.

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