简体   繁体   中英

sql query ascending or descending order

In my project I want to one query which is perform kms wise ASC|DESC query

My Query Is:

select p_kms from Places where place_type='1' ORDER BY p_kms DESC

My Out is :

p_kms
81
71
70
602
52
493
272
245
2049
1968
1948
1852
1813
1811
1807
1771
1758
174
1737
125
125
123
107
1035

Please let me know what I can do to solve this query. I use both ways ASC and DESC but not getting a satisfying result. How can I display p_KMS in ASC | DESC mode(ORDER BY p_KMS ASC|DESC) When you create an index on a column or number of columns in MS SQL Server (I'm using version 2005), you can specify that the index on each column be either ascending or descending. I'm having a hard time understanding why this choice is even here. Using binary sort techniques, wouldn't a lookup be just as fast either way? What difference does it make which order I choose?

Try this,

ORDER BY p_kms * 1 DESC 

will convert it to a number since it seems to be a text value. Or use

ORDER_BY cast(p_kms as unsigned) DESC 

Check the table definition and change it. You can change the data type to int like this

ALTER TABLE your_table MODIFY COLUMN p_kms int;

I assume that p_kms is a varchar. So you would have to cast it to a number when ordering:

select p_kms from Places where place_type='1' ORDER BY cast(p_kms as unsigned) DESC

Your query is right, But you want to change field "p_kms" as integer,

ALTER TABLE  Places MODIFY COLUMN p_kms int;

Hope u got it :) Enjoy :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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