Mysql:我需要获取查询中项目的偏移量。
我有一个图像库:每个堆栈显示 6 个图像,因此当我请求图像 22 时,它显示从 18 到 24 的图像。它应该首先获取图像 22 的偏移量,然后获取从 18 到 24 的图像。
另一个例子:我请求图像编号 62(和偏移量 62),它将选择偏移量从 60 到 66 的图像。
可以使用单个查询吗?
最重要的是获取 id 等于数字的项目的偏移值。
谢谢;)
编辑:
select * from images order_by Updated_at offset(这里我需要获取此查询中图像id的偏移量,并进行一些计算...这就是我需要的,如果可能的话..:d)
编辑2:
现在我知道我需要 2 个查询:
1°:使用自定义顺序获取查询中图像的偏移量
2°:使用第一个查询的偏移量获取图像...这个我可以单独完成,第一个是问题..:s
Mysql: i need to get the offset of a item in a query.
I have a image gallery: this show 6 image per stack, so when i request image 22 it shows images from 18 to 24. It should first get the offset of the image 22, then get the images from 18 to 24.
Another example: i request the image number 62(and offset 62), it will select images with offset from 60 to 66.
Is possible with a single query?
The main important thing is to get the offset value of the item that has its id equal to a number.
Thanks ;)
EDIT:
select * from images order_by updated_at offset(here i need to get the offset of the image id in this query, and the make some calculation... this is what i need, if is possible.. :d)
EDIT2:
Now I understand that I need 2 queries:
1º: get the offset of the image within the query with my custom order
2º: get the images using the offset from the first query... this I can make it alone, the first one is the problem.. :s
发布评论
评论(4)
如果您的图像具有连续 ID,您可能需要执行以下操作:
将上述查询中的
?
参数替换为所请求图像的 ID。更新:您的图像似乎不是按顺序 ID 排序的,而是按时间戳排序的。不幸的是,MySQL 似乎不支持
LIMIT
子句中的变量表达式 (来源)。一种选择是使用准备好的语句:另一种选择可能是:
If your images have sequential IDs, you may want to do the following:
Replace the
?
parameter in the above query with the ID of the image requested.UPDATE: It seems that your images are not ordered by a sequential ID, but by a timestamp. Unfortunately it looks like MySQL does not support variable expressions in the
LIMIT
clause (Source). One option would be to use a prepared statement:Another option could be:
编辑:看来MySQL不允许在OFFSET子句中使用这种表达式。如果您可以使用预准备语句(直接使用 SQL 或其他语言),则可以预先进行计算并使用它。请参阅丹尼尔对此的回答。由于其他有用的信息,我将这个答案留在这里。
您正在寻找的称为分页。在 MySQL 中,您可以使用
LIMIT
和OFFSET
关键字来完成此操作:将
?
替换为请求的图像索引。请注意,当您请求图像 62 时,这将给出偏移量为 60、61、62、63、64、65 的图像。我假设您在示例中给出的最后一个偏移量是唯一的。如果我做出了错误的假设,你应该做出相应的调整。还有一点解释:
LIMIT
使查询仅返回给定数量的结果。因为你总是想要六个,所以这很容易。OFFSET
使查询仅返回给定偏移量的结果。该计算将整数除以六并乘以六。这会得到给定数字之前六的倍数,正是您想要的。EDIT: It appears MySQL does not allow this kind of expression in the OFFSET clause. If you can use a prepared statement (either directly in SQL or in another language), you can make the calculation beforehand and use it. See Daniel's answer for that. I'm leaving this answer here because of the other useful information.
What you are looking for is called pagination. In MySQL you can do it with the
LIMIT
andOFFSET
keywords:Replace
?
with the requested image index. Note that this will give images with offsets 60, 61, 62, 63, 64, 65 when you ask for image 62. I assumed the last offset you gave in the examples was exclusive. You should adjust accordingly if I made the wrong assumption.And a little explanation:
LIMIT
makes the query return only the given number of results. Because you always want six, that makes it easy.OFFSET
makes the query return only results from the given offset. The calculation does integer division by six and multiplies by six. This results in the previous multiple of six of the given number, exactly what you want.我可能不明白你的意思,但是为什么所有事情都必须在 MySQL 中完成呢?
假设您使用 LAMP:
现在您已在
$pagination_start
中开始分页 [ie 60],并在$offset_mysql
中请求图像偏移量 [ie 62]:现在作为回报,您会得到一个包含 6 个图像的数组,而请求的数组位于
$result[$offset_array]
处。I may not understand you, but why do you have to do everything in MySQL?
Let's assume you use LAMP:
Now you have start of your pagination in
$pagination_start
[i.e. 60] and requested image offset [i.e. 62] in$offset_mysql
:Now in return, you get an array cotaning 6 images and the one requested is at
$result[$offset_array]
.