【问题标题】:selecting rows after the id given the order在给定顺序的 id 之后选择行
【发布时间】:2016-02-20 00:33:11
【问题描述】:

给定如下表格:

id     quantity
1      5
2      3
3      7
4      2
5      8

给定一个 id 和顺序,如果按给定顺序排序,我想选择在给定 id 之后的所有行。比如id为1,order为“quantity desc”,我想获取

id    quantity
2     3
4     2

我怎样才能只用 sql 来做到这一点。我只能想办法结合php和sql。

【问题讨论】:

    标签: mysql sql select sql-order-by


    【解决方案1】:

    您可以为此使用子查询:

    select t.*
    from t
    where quantity < (select t2.quantity from t t2 where t2.id = 1)
    order by quantity desc;
    

    【讨论】:

      猜你喜欢
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      相关资源
      最近更新 更多