gpt4 book ai didi

sql - 有点复杂的sql行位置

转载 作者:行者123 更新时间:2023-11-29 02:37:58 25 4
gpt4 key购买 nike

基本上我有相册,其中有 50 张图像初始化.. 现在如果我显示图像列表,我知道从哪到哪一行显示(显示:50 中的 20 到 30),意味着显示从 20 到 30 的 10 行.那么现在的问题是,我想选择一张图片,但仍然显示它选择了哪个位置,所以我可以来回移动,但也保持位置。

如果我选择第 5 张图像,其 ID 为“sd564”,我想显示(50 张图像中的第 6 张),意味着您看到的是 50 张图像中的第 6 张..如果我得到下一行 ID 并显示它,那么,我想展示(50 张图片中的 7 张)。

好吧,我可以从分页指针轻松完成所有这些操作,就像在 url 中说的 (after=5, after=6)... 它随位置移动,但是如果我没有这个 (after=6) 并且只有一个 ID,我怎么还能这样做?

我不想使用 (after=6) 也因为它的动态站点和图像添加和删除,所以位置更改和与其他人共享并返回相同的旧链接,那么它会是错误的位置。

我应该为此运行什么样的 sql 查询?

目前我有

select * from images where id = 'sd564'; 

显然我需要在查询中添加限制或其他一些东西来获得我想要的,或者可能运行另一个查询来获得结果,同时也保留这个旧查询。反正我只是想要定位。我希望你能帮我解决这个问题

示例: http://media.photobucket.com/image/color%20splash/aly3265/converse.jpg

sample http://img41.imageshack.us/img41/5631/viewing3of8240.png

相册查询请求(检查下面的帖子)

select images.* from images, album
where album_id = '5'
and album_id = image_album_id
order by created_date DESC
limit ....;

最佳答案

假设created_date根据 album_id 是唯一的并且 ( album_id , created_date ) 对于 images 中的所有行都是唯一的,然后是:

select     i1.*, count(*) as position
from images i1
inner join images i2
on i1.album_id = i2.album_id -- get all other pics in this album
and i1.created_date >= i2.created_date -- in case they were created before this pic
where i1.album_id = 5
group by i1.created_date

将可靠地为您提供图像及其位置。请理解,这只有在 (album_id,created_date) 在整个图像表中是唯一的情况下才能可靠地工作。如果不是这种情况,该位置将不可靠,并且由于 GROUP BY,您可能看不到所有照片。 .另请注意 GROUP BY像这样的子句,只列出出现在 SELECT 中的一些列列表(在本例中为 images.* )在大多数 RDBMS-es 中无效。有关该问题的详细讨论,请参阅:http://dev.mysql.com/tech-resources/articles/debunking-group-by-myths.html

这样做:

select     i1.*, count(*) as position
from images i1
inner join images i2
on i1.album_id = i2.album_id -- get all other pics in this album
and i1.created_date >= i2.created_date -- in case they were created before this pic
where i1.album_id = 5
group by i1.created_date
having count(*) = 4

您选择第 4 个位置的图像(注意 having count(*) = 4)

这样做:

select     i1.*, count(*) as position
from images i1
inner join images i2
on i1.album_id = i2.album_id -- get all other pics in this album
and i1.created_date >= i2.created_date -- in case they were created before this pic
where i1.album_id = 5
group by i1.created_date
having count(*) between 1 and 10

您选择位置为 1 到 10 的所有照片(再次注意 having 子句。)

当然,如果你只想要一张特定的图片,你可以简单地这样做:

select     i1.*, count(*) as position
from images i1
inner join images i2
on i1.album_id = i2.album_id -- get all other pics in this album
and i1.created_date >= i2.created_date -- in case they were created before this pic
where i1.image_id = 's1234'
group by i1.created_date

这将正确报告图像在相册中的位置(当然,假设 image_id 在图像表中是唯一的)。在这种情况下,您不需要 having 子句,因为您已经确定了所需的图像。

关于sql - 有点复杂的sql行位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2036425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com