之后的SQL顺序超过

时间:2017-05-09 04:26:31

标签: mysql sql

我正在尝试使用SQL对结果进行排序

id | name (table name: student)
-----
1 | jhon
2 | amin
3 | heli
4 | mir
5 | mrs
6 | amr
7 | jonnathan
8 | adhy

当我使用此查询时

select id from studenth where id>='3' order by id DESC limit 2

出现的结果是

id | name (table name: student)
-----
8 | adhy
7 | jonnathan

虽然我想在id = 3之后对结果进行排序,但我希望数据如下

id | name (table name: student)
-----
4 | mir
3 | heli

7 个答案:

答案 0 :(得分:1)

<div>
<p>{this.props}</p>
</div>

答案 1 :(得分:0)

您可以执行以下操作:

select * from student where id>=3 order by id LIMIT 2

如果你想记录并按降序排序,那么你可以去寻找子查询。

SELECT * FROM
( SELECT * 
  FROM student 
  WHERE id>=3 ORDER BY id LIMIT 2) X
ORDER BY X.id DESC

您的查询问题

select * from student where id>='3' order by id DESC limit 2

上述查询将使用id=3或更多的结果获取所有结果,并按降序排序,而LIMIT 2只会显示2条记录。

这就是它显示最后2条记录的原因。

答案 2 :(得分:0)

删除此var measurement = new esri.dijit.Measurement({ map: map, //measurement.hideTool("location") // measurement.hideTool("distance") }, dojo.byId('measurementDiv')); measurement.startup(); measurement.hide(); measurement.hideTool("location"); measurement.hideTool("distance"); //measurement.hideTool("measurement Result"); <div class="roundedCorners" id="measureWindow" > <div class="innerDiv roundedCorners"> <div id="measurementDiv"></div> </div> </div>

中的单引号
id>='3'

更新1:

按顺序排序限制2问题在这里使用子查询得到结果然后应用降序。

select id from student where id>=3 order by id desc limit 2

答案 3 :(得分:0)

您可以使用此查询。你希望所有的东西都小于或等于4而不是3之后的所有东西。

Gradle

答案 4 :(得分:0)

如果我理解正确,您希望数据按照id + 1记录的降序排列。查询应该如下所示:

select id from studenth where id<='4' order by id DESC limit 2;

答案 5 :(得分:0)

尝试使用使用子查询,它易于理解并符合您的要求:

SELECT id, name 
FROM studenth 
WHERE id IN (SELECT id 
             FROM studenth 
             WHERE id >= 3 ORDER BY id LIMIT 2) ORDER BY id DESC

答案 6 :(得分:0)

请尝试以下方法......

SELECT id,
       name
FROM tblTable
WHERE id >= 3
ORDER BY id DESC
LIMIT ( ( SELECT COUNT( * )
          FROM tblTable
          WHERE id >= 3 ) - 1 ), 2;

此声明的工作原理是根据tblTable的值对id的记录进行排序。然后,它使用LIMIT从排序列表中选择特定记录。如果有20个eligibnle记录,这相当于LIMIT 19, 2,这将为我们提供第19和第20条记录。

如果您有任何问题或意见,请随时发表评论。