In MySQL, the field ID in the table is A1, A2 ,... A22.. Sorting Problem

Source: Internet
Author: User
Someone asked, for example, if the data table contains sortid characters ranging from A1 to A29. If "order by sortid" is added to the query, the sorting will be A1, A10, a11... a19, A2 ,.. a29, A3, a4... a9
How can we make it output in the order of A1, A2, a3... A28, A29?

Create a test table
Create Table 'test _ njj '(
'Id' int (10) Not null auto_increment,
'Sortid' varchar (20) not null,
'User _ id' int (10) Not null default '0 ',
Primary Key ('id ')
) Engine = InnoDB default charset = utf8;

We only need to use MySQL built-in functionsSubstring (STR, POS)Split sortid.
SelectSubstring (sorid, 2) from test_njj
We can see that a has been filtered out, but the sorting is still incorrect, because although the split number is a number, it is still considered as a string, so it is necessary to convert it into a number to make
Substring (sorid, 2) * 1
Therefore
Select * From test_njj order by substring (sortid, 2) * 1;
You can.
The above test passed in mysql4.1.18

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.