On 24 Apr 2013, at 10:09pm, Gary Baranzini <cona...@gmail.com> wrote:

> I have the following query:
> 
> SELECT id  FROM pointslocation  WHERE id IN (1,7,3,4,5,2,6)
> 
> What 's returned is 1,2,3,4,5,6,7.
> 
> How do I retain the order in the IN list?

There's no simple way to do that in SQL.  The numbers in the brackets is a set: 
an unordered collection.  A number is either in the list or not, the list has 
no inherent order.

If I wanted to select all rows from pointslocation in a particular order I'd 
probably make another column in pointslocation with the ordinals in.

If I wanted to select just a few records in that order I'd make another TABLE 
(possibly using CREATE TEMPORARY TABLE) which had the ordinals in ...

pl_id   sel_order
1       1
7       2
3       3
4       4
5       5
2       6
6       7

Then I'd use a SELECT JOIN to pull up the pointslocation rows in that order.  
Possibly something like

SELECT pointslocation.* FROM myTable JOIN pointslocation ON 
pointslocation.id=myTable.pl_id ORDER BY myTable.sel_order

but I can't test the above here so don't assume it's definitely going to work.

Simon.
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to