在DB中选择下一项而不知道它具有什么ID [英] Select next item in DB without knowing what ID it has

查看:61
本文介绍了在DB中选择下一项而不知道它具有什么ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Access DB,我将从中提取图像。每个图像

都有一个相关的ID,但ID'不一定是顺序的(一些

图像可能已被删除,在ID列表中留下空白'' s)。


我希望能够从数据库中调用一个ID及其图像,

但是还返回了之前的ID和下一个ID ID,即使那些

实际ID'不一定是+1和-1被调用的ID。


例如,我想要出现ID为22的图像。*有*

是ID为21,23和24的图像,但它们已被删除了
$ b $过去。我希望能够在相同的查询中返回(或者能够立即使用另一个查询来发现它们的
)ID为20和

25,因为这些是当前现有照片的ID',直接在ID为22的照片之前和之后是




是在哪里我可以看到和使用图像的例子

存储在数据库中?


TIA

... .Geshel

-

****************************** ******************** *********************

*我的回复是一个自动监控的垃圾邮件蜜罐。除非你想被SpamCop列入黑名单,否则不要使用*

*它。请以我的姓氏dot org回复我的*

*名字。 *

***************************************** ********* *********************

解决方案

你可以使用TOP& UNION关键字


从表中选择id,其中id = @id union

从表中选择前1个id,其中id< @id union

从表中选择前1个id,其中id> @id


HTH,


Neo Geshel <去**** @ geshel.org>在消息中写道

news:ev ************** @ tk2msftngp13.phx.gbl ...

我有一个Access DB ,我将从中拉出图像。每个图像都有一个相关的ID,但ID不一定是连续的(某些图像可能已被删除,在ID列表中留下空白)。
<我希望能够从数据库中调用一个ID及其Image,但是
也返回了之前的ID和下一个ID,即使这些ID不是必须为被调用的ID +1和-1。

例如,我想调出ID为22的图像。*有*
是ID为''的图像21,23和24的s,但它们在过去被删除了。我希望能够在相同的查询中返回(或者能够立即使用另一个查询来发现它们)ID为20和25,
因为这些是ID现有的照片直接在ID为22的照片之前和之后。

我可以在哪里看到和使用图像的示例存储在数据库中?

TIA
... Geshel
-
***************** ********************************* ***************** ****
*我的回复是一个自动监控的垃圾邮件蜜罐。除非您想被SpamCop列入黑名单,否则请勿使用*
*它。请以我的姓氏dot org回复我的*
*名字。 *
********************************************* ***** *********************



Onin Tayson写道:< blockquote class =post_quotes>Neo Geshel <去**** @ geshel.org>在消息中写道
新闻:ev ************** @ tk2msftngp13.phx.gbl ...

我有一个Access DB ,我将从中拉出图像。每个图像都有一个相关的ID,但ID不一定是连续的(某些图像可能已被删除,在ID列表中留下空白)。
<我希望能够从数据库中调用一个ID及其Image,但是
也返回了之前的ID和下一个ID,即使这些ID不是必须为被调用的ID +1和-1。

例如,我想调出ID为22的图像。*有*
是ID为''的图像21,23和24的s,但它们在过去被删除了。我希望能够在相同的查询中返回(或者能够立即使用另一个查询来发现它们)ID为20和25,
因为这些是ID现有的照片直接在ID为22的照片之前和之后。

我可以在哪里看到和使用图像的示例存储在数据库中?

TIA
... Geshel
-
***************** ******************************* ******************* ****
*我的回复是一个自动监控的垃圾邮件蜜罐。除非您想被SpamCop列入黑名单,否则请勿使用*
*它。请以我的姓氏dot org回复我的*
*名字。 *
********************************************* *** ***********************



你可以使用TOP& UNION关键字

从表中选择id,其中id = @id union
从表中选择前1个id,其中id< @id union
从表中选择前1个id,其中id> @ id

HTH,



(为清晰起见,底部张贴: http://www.caliburn.nl/topposting.html


你知道,我没有'根本没想到。虽然我很好奇,但是这个方式我读了第三行,它不会给我#25,而是最后一个ID

(编号最高的ID)在图像列表中。


虽然我不是SQL大师,但不应该是第二行和第三行

ORDER BY [ID ],第二行正常(ASC)和第三行

是否相反(DESC)?这样,第二行的最高数字是22岁以下的最高数量
,而第三行的最高数字是22岁以上最低的数字。

顶部。


同样,由于返回了三个ID,他们必须在重新插入转发器之前重新命名为

,不是吗?


我看到查询为:


SELECT TOP 1 [ID] AS [上一页] FROM [tblImages] WHERE [ID]< @id SORT BY [ID]

ASC UNION

SELECT TOP 1 [ID] AS [next] FROM [tblImages] WHERE [ID]> @id SORT BY [ID]

DESC


这样,所请求图像的ID用于从中提取另外两个
ID'数据库。一个ID是它下面最高的ID,另一个是
是它上面的最低ID。因为一次从数据库中提取多个ID

,所以必须将它们重命名,这样它们才不会发生冲突。


请告知我,如果我在这里遗漏了什么。 (顺便说一句,图片的ID将会是已知的。
的内容是前者的ID和下一张图片的

,所以他们可以连接并提供连续图像

系列。)


.... Geshel

-

********************************************** **** *********************

*我的回复是一个自动监控的垃圾邮件蜜罐。除非你想被SpamCop列入黑名单,否则不要使用*

*它。请以我的姓氏dot org回复我的*

*名字。 *

***************************************** ********* *********************


是的,我错过了ORDER BY(SORT)关键字(我只是在这里直接输入

,就像我现在正在做的那样;)。但它应该是另一种方式(基于

读取SQL - 没有在这里完成测试)。以前的ID应该是

ORDERed BY [ID] DESC和下一个ID应该是ASC订购的。下面应该是

你的SQL代码。


SELECT [ID] FROM [tblImages] WHERE [ID] = @id UNION

SELECT TOP 1 [ID] FROM [tblImages] WHERE [ID]< @id ORDER BY [ID] DESC UNION

SELECT TOP 1 [ID] FROM [tblImages] WHERE [ID]> @id ORDER BY [ID]


关于列重命名...你不必将UNIONed列重命名为

它将无效在查询结果中,它仍将使用第一个SELECT中的

列名。


HTH,


Neo Geshel <去**** @ geshel.org>在消息中写道

新闻:eB ************** @ TK2MSFTNGP12.phx.gbl ...

Onin Tayson写道:< blockquote class =post_quotes>Neo Geshel <去**** @ geshel.org>在消息中写道
新闻:ev ************** @ tk2msftngp13.phx.gbl ...

我有一个Access DB ,我将从中拉出图像。每个图像都有一个相关的ID,但ID不一定是顺序的(某些图像可能已被删除,在ID列表中留下空白)。
<我希望能够从数据库中调用一个ID及其图像,
但也返回了之前的ID和下一个ID,即使那些
实际ID不是必须为被调用的ID +1和-1。

例如,我想调出ID为22的图像。*有*
是ID为''的图像21,23和24的s,但它们在过去被删除了。我希望能够在相同的查询中返回(或者能够立即使用另一个查询来发现它们)ID为20和25,
因为这些是ID'现有的照片直接在ID为22的照片之前和之后。

我可以在哪里看到和使用图像的示例存储在数据库中?

TIA
... Geshel
-
***************** ****************************** ******************** ****
*我的回复是一个自动监控的垃圾邮件蜜罐。除非您想被SpamCop列入黑名单,否则请勿使用*
*它。请以我的姓氏dot org回复我的*
*名字。 *
********************************************* ** ************************



你可以使用TOP& UNION关键字

从表中选择id,其中id = @id union
从表中选择前1个id,其中id< @id union
从表中选择前1个id,其中id> @ id

HTH,


(为了清晰起见,底部张贴: http://www.caliburn.nl/topposting.html

你知道,我根本没有想到这一点。虽然我很好奇,我读第三行的方式,它不会给我#25,而是图像列表中的最后一个ID(
编号最高的ID)。 />
虽然我不是SQL大师,但第二和第三行不应该是ORDER BY [ID],其中第二行是正常的(ASC),第三行是
逆转(DESC)?这样,第二行的最高数字在22下方最高,而第三行在最高位置的数字最低,超过22。

同样,因为三个ID'' s正在退回,他们必须在重新插入转发器之前重新命名,不是吗?

我认为查询是:

SELECT TOP 1 [ID] AS [prev] FROM [tblImages] WHERE [ID]< @id SORT BY [ID]
ASC UNION
SELECT TOP 1 [ID] AS [next] FROM [tblImages] WHERE [ID]> @id SORT BY [ID]
DESC

这样,所请求图像的ID用于拉出两个以上的ID ID来自数据库。一个ID是它下面最高的ID,另一个是它上面最低的ID。因为一次从
数据库中提取多个ID,所以必须重新命名,这样才不会发生冲突。

请告诉我这里是否遗漏了什么。 (顺便说一下,图片的ID已经知道了。拉出来的是上一张和下一张图片的ID',因此它们可以链接到并提供连续的图像系列。)

... Geshel
-
************************* ************************* *********************
*我的回复是一个自动监控的垃圾邮件蜜罐。除非您想被SpamCop列入黑名单,否则请勿使用*
*它。请以我的姓氏dot org回复我的*
*名字。 *
********************************************* ***** *********************



I have an Access DB, from which I am going to pull images. Each image
has an associated ID, but the ID''s are not necessarily sequential (some
images may have been deleted, leaving gaps in the list of ID''s).

I am looking to be able to call an ID and its Image from the database,
but also have returned the previous ID and the next ID, even when those
actual ID''s are not necessarily +1 and -1 to the ID being called.

For example, I want to bring up an image with the ID of 22. There *had*
been images with ID''s of 21, 23 and 24, but they had been deleted in the
past. I want to be able to have returned, in the same query (or be able
to immediately use another query to discover them) the ID''s of 20 and
25, because these are the ID''s of currently existing photos that are
directly before and after the photo that has the ID of 22.

Is there anywhere where I can see and example that makes use of images
stored in a database?

TIA
....Geshel
--
************************************************** *********************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
************************************************** *********************

解决方案

you can use the TOP & UNION keywords

select id from table where id = @id union
select top 1 id from table where id<@id union
select top 1 id from table where id>@id

HTH,

"Neo Geshel" <go****@geshel.org> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...

I have an Access DB, from which I am going to pull images. Each image has
an associated ID, but the ID''s are not necessarily sequential (some images
may have been deleted, leaving gaps in the list of ID''s).

I am looking to be able to call an ID and its Image from the database, but
also have returned the previous ID and the next ID, even when those actual
ID''s are not necessarily +1 and -1 to the ID being called.

For example, I want to bring up an image with the ID of 22. There *had*
been images with ID''s of 21, 23 and 24, but they had been deleted in the
past. I want to be able to have returned, in the same query (or be able to
immediately use another query to discover them) the ID''s of 20 and 25,
because these are the ID''s of currently existing photos that are directly
before and after the photo that has the ID of 22.

Is there anywhere where I can see and example that makes use of images
stored in a database?

TIA
...Geshel
--
************************************************** *********************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
************************************************** *********************



Onin Tayson wrote:

"Neo Geshel" <go****@geshel.org> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...

I have an Access DB, from which I am going to pull images. Each image has
an associated ID, but the ID''s are not necessarily sequential (some images
may have been deleted, leaving gaps in the list of ID''s).

I am looking to be able to call an ID and its Image from the database, but
also have returned the previous ID and the next ID, even when those actual
ID''s are not necessarily +1 and -1 to the ID being called.

For example, I want to bring up an image with the ID of 22. There *had*
been images with ID''s of 21, 23 and 24, but they had been deleted in the
past. I want to be able to have returned, in the same query (or be able to
immediately use another query to discover them) the ID''s of 20 and 25,
because these are the ID''s of currently existing photos that are directly
before and after the photo that has the ID of 22.

Is there anywhere where I can see and example that makes use of images
stored in a database?

TIA
...Geshel
--
************************************************ ***********************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
************************************************ ***********************


you can use the TOP & UNION keywords

select id from table where id = @id union
select top 1 id from table where id<@id union
select top 1 id from table where id>@id

HTH,


(bottom-posted for clarity: http://www.caliburn.nl/topposting.html)

You know, I didn''t think of that at all. Although I am curious, the way
I read the third line, it wouldn''t bring me #25, but rather the last ID
(the highest numbered ID) in the list of images.

Although I''m not an SQL guru, shouldn''t the second and third lines be
ORDER BY [ID], where the second line is normal (ASC) and the third line
is reversed (DESC)? That way, the second line has the highest number
under 22 at the top, and the third line has the lowest number over 22 at
the top.

As well, since three ID''s are being returned, they''ll have to be renamed
before being plunked into the repeater, no?

I see the query as being:

SELECT TOP 1 [ID] AS [prev] FROM [tblImages] WHERE [ID]<@id SORT BY [ID]
ASC UNION
SELECT TOP 1 [ID] AS [next] FROM [tblImages] WHERE [ID]>@id SORT BY [ID]
DESC

This way, the ID of the image requested is being used to pull two more
ID''s from the database. One ID is the highest one beneath it, the other
is the lowest one above it. Because more than one ID is being pulled
from the database at a time, they have to be renamed so they don''t conflict.

Please tell me if I am missing something here. (BTW, the image''s ID will
already be known. What is being pulled are the ID''s for the prev and
next images, so they can be linked to and provide a continuous image
series.)

....Geshel
--
************************************************** *********************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
************************************************** *********************


yep, i missed the ORDER BY ("SORT") keyword (i just typed it here directly
as what i''m doing right now ;). but it should be the other way around (based
on reading the SQL - no testing done here). the Previous ID should be
ORDERed BY [ID] DESC and Next ID should be ordered ASC. below should be
your SQL code.

SELECT [ID] FROM [tblImages] WHERE [ID] = @id UNION
SELECT TOP 1 [ID] FROM [tblImages] WHERE [ID]<@id ORDER BY [ID] DESC UNION
SELECT TOP 1 [ID] FROM [tblImages] WHERE [ID]>@id ORDER BY [ID]

regarding column renaming... you don''t have to rename the UNIONed columns as
it will have no effect on the results of the query, it will still use the
column name from the first SELECT.

HTH,

"Neo Geshel" <go****@geshel.org> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...

Onin Tayson wrote:

"Neo Geshel" <go****@geshel.org> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...

I have an Access DB, from which I am going to pull images. Each image has
an associated ID, but the ID''s are not necessarily sequential (some
images may have been deleted, leaving gaps in the list of ID''s).

I am looking to be able to call an ID and its Image from the database,
but also have returned the previous ID and the next ID, even when those
actual ID''s are not necessarily +1 and -1 to the ID being called.

For example, I want to bring up an image with the ID of 22. There *had*
been images with ID''s of 21, 23 and 24, but they had been deleted in the
past. I want to be able to have returned, in the same query (or be able
to immediately use another query to discover them) the ID''s of 20 and 25,
because these are the ID''s of currently existing photos that are directly
before and after the photo that has the ID of 22.

Is there anywhere where I can see and example that makes use of images
stored in a database?

TIA
...Geshel
--
*********************************************** ************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
*********************************************** ************************


you can use the TOP & UNION keywords

select id from table where id = @id union
select top 1 id from table where id<@id union
select top 1 id from table where id>@id

HTH,


(bottom-posted for clarity: http://www.caliburn.nl/topposting.html)

You know, I didn''t think of that at all. Although I am curious, the way I
read the third line, it wouldn''t bring me #25, but rather the last ID (the
highest numbered ID) in the list of images.

Although I''m not an SQL guru, shouldn''t the second and third lines be
ORDER BY [ID], where the second line is normal (ASC) and the third line is
reversed (DESC)? That way, the second line has the highest number under 22
at the top, and the third line has the lowest number over 22 at the top.

As well, since three ID''s are being returned, they''ll have to be renamed
before being plunked into the repeater, no?

I see the query as being:

SELECT TOP 1 [ID] AS [prev] FROM [tblImages] WHERE [ID]<@id SORT BY [ID]
ASC UNION
SELECT TOP 1 [ID] AS [next] FROM [tblImages] WHERE [ID]>@id SORT BY [ID]
DESC

This way, the ID of the image requested is being used to pull two more
ID''s from the database. One ID is the highest one beneath it, the other is
the lowest one above it. Because more than one ID is being pulled from the
database at a time, they have to be renamed so they don''t conflict.

Please tell me if I am missing something here. (BTW, the image''s ID will
already be known. What is being pulled are the ID''s for the prev and next
images, so they can be linked to and provide a continuous image series.)

...Geshel
--
************************************************** *********************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
************************************************** *********************



这篇关于在DB中选择下一项而不知道它具有什么ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆