Mysql varchar sorting CAST, CONVERT function type conversion, varcharconvert

Source: Internet
Author: User

Mysql varchar sorting CAST, CONVERT function type conversion, varcharconvert

When creating a table on your own, you should create a field type as varchar (2). In fact, it should be set as integer (2). But today, we found a Sorting Problem in the background and a varchar Sorting Problem, so how can this problem be solved?

Example Table Structure: Let's take a look, my table structure show create table cardserver \ G ***************************** 1. row *************************** Table: cardserverCreate Table: create table 'cardserver' ('id' int (11) not null default '0', 'ver 'int (11) default NULL, 'createtime' datetime default NULL, 'updatetime' datetime default NULL, 'game _ id' int (2) not null default '0', 'server _ id' varchar (2) not null default '', 'server _ name' Varchar (40) not null default '', primary key ('id'), unique key 'game _ id_server_id '('game _ id', 'server _ id '), unique key 'game _ id_server_name '('game _ id', 'server _ name') ENGINE = InnoDB default charset = gbk1 row in set (0.00 sec) because foreign keys exist, I don't want to change the field type. Haha. At last, I chose to change the field type. Because what I want to explain in this log is the varchar Sorting Problem. So I will not explain how I changed the field type. If you are interested, you can search for my previous logs. (Cheat click) symptom Description: below, I sort the order of server_id in 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 | + ----------- + very obviously, the expected result is 10, 8, 7, 6, and 5. But this 10 is behind 2. Sort by string. In fact, I want to arrange it as a numerical value. Manual Conversion Type: Use the following method to sort the data after adding server_id + 0. 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 mySQL function CAST/CONVERT: mysql provides two types of conversion functions: CAST and CONVERT. How can we let go of the ready-made stuff? CAST () and CONVERT () functions can be used to obtain values of one type and generate values 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; + ----------- + | server_id | + ----------- + | 10 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | + ----------- + you can use CONVERT to solve this problem: select server_id from cardserver where game_id = 1 or Der by CONVERT (server_id, SIGNED) desc limit 10; + ----------- + | server_id | + ----------- + | 10 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | + ----------- + Summary: all roads pass Rome and all small roads pass my house. No matter what the solution is, it is a good solution to the problem. Of course, since MySQL provides us with ready-made functions. Why don't we make the Code look more beautiful? Haha. Therefore, we recommend that you use the CAST or CONVERT function to sort MySql varchar.


Related Article

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.