Two implementations:

CREATE TABLE t (a int, b int);
INSERT INTO t VALUES(1,1);
INSERT INTO t VALUES(2,4);
INSERT INTO t VALUES(3,9);
INSERT INTO t VALUES(4,16);
INSERT INTO t VALUES(5,25);
INSERT INTO t VALUES(6,36);
INSERT INTO t VALUES(7,49);
INSERT INTO t VALUES(8,64);
INSERT INTO t VALUES(9,81);
INSERT INTO t VALUES(10,100);
INSERT INTO t VALUES(11,121);
INSERT INTO t VALUES(12,144);
INSERT INTO t VALUES(13,169);
INSERT INTO t VALUES(14,196);
INSERT INTO t VALUES(15,225);
INSERT INTO t VALUES(16,256);
INSERT INTO t VALUES(17,289);
INSERT INTO t VALUES(18,324);
INSERT INTO t VALUES(19,361);
INSERT INTO t VALUES(20,400);
INSERT INTO t VALUES(21,441);

SELECT * FROM t WHERE a IN (5, 3, 10) 
ORDER BY CASE a WHEN 5 THEN 1 WHEN 3 THEN 2 WHEN 10 THEN 3 ELSE -1 END;

SELECT a, b FROM (
    SELECT (CASE a WHEN 5 THEN 1 WHEN 3 THEN 2 WHEN 10 THEN 3 ELSE -1 END) o,
    a, b FROM t
) t WHERE o>= 0 ORDER BY o;


Both yield:
a           b         
----------  ----------
5           25        
3           9         
10          100       


The first is simpler, but requires you to list your items twice.
The second fails to use an index, but only names the items once.

Try the first unless you know a full table scan is acceptable.

--David Garfield

Michael Stephenson writes:
> Wondering if anyone has a way to execute a query that selects rows based on
> a list of rowids and returns the results in the order of the rowids passed
> in.
> 
> Thanks,
> 
> ~Mike
> 
> _______________________________________________
> sqlite-users mailing list
> [email protected]
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to