Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite - sorting a table

Tags:

sqlite

I have a database in SQLlite and I'd like to sort my table in an alphabetical order. How can I do it? Is there a way to sort entries using only SQLite or do I have first to read a table into an array, sort it and afterwards to write it into a database?

Here is my query: "SELECT entry FROM table WHERE id=?" I need to get data from the table using this statement in order to get only one entry at a time.

Thank you in advance, Ilya.

like image 323
Ilya Suzdalnitski Avatar asked Mar 21 '09 20:03

Ilya Suzdalnitski


2 Answers

'Select name from table order by name asc' 

'asc' is ascending, will give you the text field in alphabetical order, conversely 'desc' will give it to you in reverse alphabetical order.

Edit: as a general rule, you should let the database do the sorting. The below post is related, and arguably, almost the same. You might find it helpful:

PHP/SQL: ORDER BY or sort($array)?

like image 155
karim79 Avatar answered Nov 23 '22 16:11

karim79


It is also worth emphasising that the order of data in any SQL database table, or retrieved from such using a query that does not include a order by clause, is not defined.

In practice a straight read of a table without an order by will retrieve data in some fixed order and it's often the order of insert. However to rely on this is always an error, although one that is seen alarmingly often.

like image 28
Cruachan Avatar answered Nov 23 '22 15:11

Cruachan