从tbl中选择*,其中id> = 5/*,然后将ID< 5中的结果相加以查询*/ [英] Select * from tbl where id>=5 /* then add the result from id<5 to query */
问题描述
我有一张9行的表格.
I have a table with 9 rows.
$ id = 5
如果我使用SELECT * FROM tbl WHERE id>=$id
我得到5,6,7,8,9行.在此查询中,我想添加SELECT * FROM tbl WHERE id<$id
的结果,这样我将获得最后的5、6、7、8、9、1、2、3、4行.
$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.
这是为了避免在数据库中访问两次,然后将结果集添加到php中.
This is to avoid going two times in database then add the result set in php.
是的,顺序很重要.谢谢你们的快速反应.谢谢@knittl(可接受的答案)和@Swanand的最佳答案.
Yes order is important. Thank you guys for the fast response. Thank you @knittl(Accepted answer) and @Swanand for the best 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
这篇关于从tbl中选择*,其中id> = 5/*,然后将ID< 5中的结果相加以查询*/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!