Select * from tbl, where id> = 5 / *, then add the result from id <5 to the query * /

I have a table with 9 rows.

$ ID = 5
If I use SELECT * FROM tbl WHERE id>=$id, I get 5,6,7,8,9 lines. To this query, I want to add the result SELECT * FROM tbl WHERE id<$idto get the final rows 5,6,7,8,9,1,2,3,4.

This is to avoid two times in the database, and then add the result set to php.

EDIT: Yes, order is important. Thanks guys for the quick reply. Thanks to @knittl (Accepted answer) and @Swanand for the best answers.

+5
source share
4 answers

Do you need all the lines? if the order is what you are looking for, sort your result set:

SELECT * FROM tbl
ORDER BY id >= $id DESC, id ASC
+6

- ...

SELECT *, (IF(id<5, true,false)) AS ltfive
FROM mytable
ORDER BY ltfive, id
+1

UNION - !

(SELECT *,1 as q FROM tbl WHERE id>=$id)
UNION
(SELECT *,2 as q FROM tbl WHERE id<$id)
ORDER BY q, id;
-1

$id=5;
SELECT * FROM tbl WHERE id>=$id
union
SELECT * FROM tbl WHERE id<$id ;
-1
source

All Articles