是否可以只用一个查询来检索分成三个表的对象?

发布于 2024-12-01 15:32:34 字数 438 浏览 0 评论 0原文

编辑:答案有效,但会大大减慢查询速度。还有其他解决方案吗?


我的程序中有以下对象。

Page 具有各种 TextBlock,它们具有各种 Token

这三者存储在数据库中的三个表中,对象名称相同。

我想知道是否可以仅通过一个查询来恢复填充程序中的对象所需的所有数据。

我只需要一个查询,因为我使用 CURSOR 来迭代数据库中存储的所有对象。

数据库方案是这样的:

Page(id, someAttributes)
TextBlock(id,pageId, someAttributes)
令牌(textblockId, someAttributes)

EDIT: The answers works, but slow down the query a lot. Is there another solution?


I have the following object in my program.

Page haves various TextBlock that haves various Token.

The three are stored in the database in three tables with the same namas of the objects.

I want to know if is possible to recover all the data needed to fill the object in the program with only one query.

I want only one query because I'm using a CURSOR to iterate over all the objects stored in the database.

The database scheme is this:

Page(id, someAttributes)
TextBlock(id,pageId, someAttributes)
Token(textblockId, someAttributes)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

堇色安年 2024-12-08 15:32:34

假设每个页面至少有一个文本块,每次尝试至少有一个令牌

SELECT * FROM
Page AS P 
INNER JOIN TextBlock T ON T.pageId = P.id 
INNER JOIN Token TK ON TK.textblockId = T.id;

替代方案(旧语法):

SELECT * FROM
Page AS P, TextBlock T, Token TK WHERE 
T.pageId = P.id AND
TK.textblockId = T.id;

如果上述条件可能并不总是成立,那么您需要使用OUTER JOIN 而不是 INNER JOIN

Assuming that every Page has at least one Textblock with at least one Token each try

SELECT * FROM
Page AS P 
INNER JOIN TextBlock T ON T.pageId = P.id 
INNER JOIN Token TK ON TK.textblockId = T.id;

Alternatively (old syntax):

SELECT * FROM
Page AS P, TextBlock T, Token TK WHERE 
T.pageId = P.id AND
TK.textblockId = T.id;

IF it is possible that the above condition is not always true then you need to use OUTER JOIN instead of INNER JOIN.

-柠檬树下少年和吉他 2024-12-08 15:32:34

是的,可以通过 3 路连接实现。

在这种情况下,每个令牌您将获得一行。

因此,您需要循环并从游标中获取所有行,并在看到新的页面 id、TextBlock id 等时适当地创建对象。

注意:您需要使用外连接,以防 TextBlock 没有令牌,因此仍会检索 TextBlock(Token 属性为空值)。对于没有 TextBlock 的页面也是如此。

Yes it is possible with a 3 way join.

You'll get one row per Token in this case.

So you'll need to loop and fetch all rows from the cursor and appropriately create the objects as you see new Page ids, TextBlock ids, etc.

NOTE: You'll need to use outer joins, in case a TextBlock has no Tokens, so the TextBlock would still be retrieved (with null values for Token attributes). Same for Pages with no TextBlock.

枫林﹌晚霞¤ 2024-12-08 15:32:34
Select 
   Page.someAttributes,
   TextBlock.someAttributes,
   Token.someAttributes
From
   Page,
   TextBlock,
   Token
Where 
   Page.id = TextBlock.pageId
AND
   TextBlock.id = Token.textblockId
Group By 
   Page.id
Select 
   Page.someAttributes,
   TextBlock.someAttributes,
   Token.someAttributes
From
   Page,
   TextBlock,
   Token
Where 
   Page.id = TextBlock.pageId
AND
   TextBlock.id = Token.textblockId
Group By 
   Page.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文