简体   繁体   中英

Ordering of items from database using SQL when the item is a Text

I am writing an Iphone app and using sqlite as my database. I have 10 objects and I need to order them by their category id. All the id are numbers (starting from 1 to 10).

The sql statement looks like this: SELECT * FROM PACKAGE ORDER BY CATEGORYID ASC

When I query, the result shows 1,10,2,3,4....9.

How do i solve this?

maybe it's because that the categoryID is TEXT

SELECT * FROM PACKAGE ORDER BY CAST(CategoryID as INTEGER) ASC;

The issue seems to be with the data type of category id if its character then you will get the order as shown above. but if the data type is numeric you will the it in correct format

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