可以将LIMIT与子查询结果一起使用吗?

编程入门 行业动态 更新时间:2024-10-25 19:32:22
本文介绍了可以将LIMIT与子查询结果一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当需要有序集的最后一行时,通常会创建派生表并重新排序.例如,要以自动递增的id返回表的最后3个元素:

When the last rows of an ordered set are required, it is usual to create a derived table and reorder. For example, to return the last 3 elements of a table with an auto incremented id:

SELECT * FROM ( SELECT * FROM table ORDER BY id DESC LIMIT 3 ) t ORDER BY t.id

由于LIMIT也可以具有偏移量,因此如果我们提前知道此查询的行数(例如10),则可以实现相同的结果:

Since LIMIT can also have an offset, the same result could be achieved if we know the number of lines (say 10) in advance with this query:

SELECT * FROM table ORDER BY id LIMIT 3 OFFSET 7

是否可以对表进行count(*)子查询并使用该数字动态构建LIMIT?

Is it possible to run a subquery to count(*) a table and dynamically build a LIMIT using that number?

SELECT * FROM table ORDER BY id LIMIT 3 OFFSET [ select count() -3 ]

推荐答案

否,无法指定动态偏移量.

No, it isn't possible to specify a dynamic offset.

使用子查询的原始查询是最简单的方法.

Your original query with a subquery is the easiest way to do this.

更多推荐

可以将LIMIT与子查询结果一起使用吗?

本文发布于:2023-10-17 13:17:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1501032.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:查询结果   与子   LIMIT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!