选择给定顺序的 id 之后的行
selecting rows after the id given the order
给出如下 table:
id quantity
1 5
2 3
3 7
4 2
5 8
给定一个 id 和顺序,我想 select 如果按给定顺序排序,给定 id 之后的所有行。比如id是1,顺序是"quantity desc",我想得到
id quantity
2 3
4 2
我怎样才能只用 sql 做到这一点?只能想办法结合php和sql。
您可以为此使用子查询:
select t.*
from t
where quantity < (select t2.quantity from t t2 where t2.id = 1)
order by quantity desc;
给出如下 table:
id quantity
1 5
2 3
3 7
4 2
5 8
给定一个 id 和顺序,我想 select 如果按给定顺序排序,给定 id 之后的所有行。比如id是1,顺序是"quantity desc",我想得到
id quantity
2 3
4 2
我怎样才能只用 sql 做到这一点?只能想办法结合php和sql。
您可以为此使用子查询:
select t.*
from t
where quantity < (select t2.quantity from t t2 where t2.id = 1)
order by quantity desc;