On Fri, 2010-03-26 at 10:00 +0100, Fredrik Karlsson wrote:
> Hi,
> 
> I have a list of id:s stored in a field. I would now like to get some
> information from a table by these id:s, but exactly in this order. So,
> if I have a table
> 
> 1 One
> 2 Two
> 3 Three
> 
> and the sequence "3,1,2" stored somewhere, how do I get a neat list like
> 
> Three
> One
> Two
> 
> ?
> I I can see a solution where I split the string outside of sqlite and
> then construct a query consisting of lots of UNIONs, like (not a full
> example, just an illustration)
> 
> (select name from mytab where id = 3)
> UNION ALL
> (select name from mytab where id = 1)
> UNION ALL
> (select name from mytab where id = 2)
> ...
> ...
> 
> but maybe there is a better option somewhere?
> I would be thankful for any help I can get.
> 
> /Fredrik
> 

I'd create a second table
CREATE TABLE sequence (seq integer primary key, id);

Then insert your desired sequence in to that table
seq | id
1   | 3
2   | 1
3   | 2

Select * From datatable, sequence where datatable.id = sequence.id order
by sequence.seq;

David

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

Reply via email to