简体   繁体   English

如何按升序对这些数据进行排序?

[英]how to sort this data by in ascending order?

Am trying to retrieve data from Database in Ascending order. 我试图以升序从数据库中检索数据。

My query was 我的查询是

select pid 
from `patient_list` t1, patient_info t2 
where pid > '2000' and t2.id=t1.id 
order by pid asc 
limit 10

but the data is like 但是数据就像

pid
2221
2223
2224
2227
**223**
2238
2239
2242
2245
2247
**225**

How to sort this ? 如何排序?

Your data is sorted alphanumeric. 您的数据按字母数字排序。 To force a numerical sort you must cast the data into a number. 要强制进行数字排序,必须将数据转换为数字。 For instance with pid * 1 例如pid * 1

select pid 
from `patient_list` t1, patient_info t2 
where pid > '2000' 
and t2.id=t1.id 
order by pid * 1 asc 
limit 1

Since your pid is of a string type you should consider changing that to an int . 由于您的pid是字符串类型,因此您应该考虑将其更改为int

your pid-columns seems not to be a numeric fieldtype, so the values are treaten as string values. 您的pid列似乎不是数字字段类型,因此这些值被视为字符串值。 in your case the order-by-algorithm is sorting the whole string char by char and not by the actual value of the number 在您的情况下,按算法排序是按char而不是数字的实际值对整个字符串char进行排序

2227, 223, 2238 will sort like AAAG, AAB, AABG 2227, 223, 2238会有点像AAAG, AAB, AABG

i assume its varchar, try to change it to INT 我假设它的varchar,尝试将其更改为INT

In order to do that, you should sort by both length of pid and itself. 为此,您应该按pid的长度及其本身进行排序。 This trick will work even for non-integer pids. 此技巧甚至适用于非整数pid。

select pid from patient_list t1, patient_info t2 
where length(pid) >= 4 and pid > '2000' and t2.id=t1.id 
order by length(pid) asc, pid asc limit 1

Firstly, clean the data. 首先,清理数据。 Secondly, cast to int. 其次,将其转换为int。 Thirdly sort and/or filter. 第三,排序和/或过滤。

select pid from `patient_list` t1, patient_info t2 
where replace(pid, "*", "") + 0 > 2000 and t2.id = t1.id 
order by replace(pid, "*", "") + 0
limit 1

Note that you are also filtering on that field so you should use the replacement logic twice. 请注意,您还在该字段上进行过滤,因此您应该使用两次替换逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM