Differences and points of attention and solutions for MySQL database sorting by varchar string type and sorting by int integer type

Source: Internet
Author: User

When you build your own table, create a field type as varchar (2), which should be built as an int (2). Because I'm only allowed to output numbers. This would have been nothing, just occupy a bit of space, too lazy to change. But today there is a problem with sorting in the background. So, there is no way to change it. Here's a brief look at MySQL's varchar sort problem, warning.

Below, I server_id order from the database, we look at the results after the sorting:

Select server_id from Cardserver where game_id = 1 order by SERVER_ID desc limit ten;

+-----------+
|server_id|
+-----------+
|8|
|7|
|6|
|5|
|4|
| 3|
|2|
|Ten|
|1|
+-----------+

Obviously, the result I want is 10,8,7,6,5. But this 10 is in the back of 2. Sorted by string. I'm actually trying to rank it as a numerical value.

Manual Conversion Type:

The following method can be used, so that the server_id+0 after the sorting, the problem solved.

Selectserver_id fromCardserverwheregame_id=1Order byserver_id+0descLimitTen;

+-----------+
|server_id|
+-----------+
|Ten |
|8|
|7|
| 6|
|5|
|4|
|3|
|2|
|1 |
+-----------+

Use the MySQL function Cast/convert:

MySQL provides us with two types of conversion functions: cast and convert, how can we let go of the ready-made stuff?

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:

Selectserver_id fromCardserverwheregame_id=1Order byCAST(server_id as signed)descLimitTen;

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 Ten ;


This article from the "Alpine" blog, reproduced please contact the author!

Differences and points of attention and solutions for MySQL database sorting by varchar string type and sorting by int integer type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.