Home >Backend Development >PHP Tutorial >求教一个MYSQL数字和字母混合排序有关问题

求教一个MYSQL数字和字母混合排序有关问题

WBOY
WBOYOriginal
2016-06-13 12:06:581149browse

求教一个MYSQL数字和字母混合排序问题
数据库里有一个表a,表a有一个字段b为字符串类型,里面的数据大多为88A242 ,88A106,88A109,92A129,92A068,2000A291,2000A216,2014A063,2014A315这样的字符串,求教各位大神,如何排序?

排序后应为: 88A106,88A109,88A242,92A068,92A129,2000A216,2000A291,2014A063,2014A315
------解决思路----------------------

create temporary table T (a varchar(10));<br /><br />insert into T (a) values ('88A242'),('88A106'),('88A109'),('92A129'),('92A068'),('2000A291'),('2000A216'),('2014A063'),('2014A315');<br /><br />select * from T order by lpad(a, 10, '0');
a <br />88A106 <br />88A109 <br />88A242 <br />92A068 <br />92A129 <br />2000A216 <br />2000A291 <br />2014A063 <br />2014A315 <br />

------解决思路----------------------
LPAD(str,len,padstr) 
返回字符串 str, 其左边由字符串padstr 填补到len 字符长度。假如str 的长度大于len, 则返回值被缩短至 len 字符。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn