Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

mysql

I have a table with 9 rows.

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

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

EDIT: Yes order is important. Thank you guys for the fast response. Thank you @knittl(Accepted answer) and @Swanand for the best answers.

like image 625
Alqin Avatar asked Oct 01 '10 12:10

Alqin


People also ask

What is SELECT T * in SQL?

Retrieves rows from the database and enables the selection of one or many rows or columns from one or many tables in SQL Server.

What is SELECT * from in SQL?

An asterisk (" * ") can be used to specify that the query should return all columns of the queried tables. SELECT is the most complex statement in SQL, with optional keywords and clauses that include: The FROM clause, which indicates the table(s) to retrieve data from.

What does the symbol * represent in a SELECT query?

You can obviously retrieve multiple columns for each record, and (only if you want to retrieve all the columns) you can replace the list of them with * , which means "all columns". So, in a SELECT statement, writing * is the same of listing all the columns the entity has.


1 Answers

you want all rows? if the order is what you are looking for, sort your result set:

SELECT * FROM tbl
ORDER BY id >= $id DESC, id ASC
like image 78
knittl Avatar answered Sep 22 '22 05:09

knittl