通过一次查询获取项目和所有相关标签

发布于 2024-09-28 14:55:01 字数 581 浏览 3 评论 0原文

我有以下表格结构:

项目:

Id,         
Description 

标签:

Id,
TagName

ItemXrefTag

 Id,
 TagId,
 ItemId

读取与某个标签和所有其他标签相关的所有项目(与该项目相关)的最佳方法是什么,以便能够显示项目列表以及与该项目相关的所有标签物品?

如果不清楚,我将给出示例:

项目,标签:

 Code complete, book|programming|cool
 Reactoring, book|programming|refactoring|cool
 C# for dummies, book|dont like it| not cool

PS 我正在使用亚音速,但由于它支持从查询获取数据,所以我可以接受查询,这将使我获取所有数据。当然,我可以执行连接并迭代多行,我将获取并制作项目集合,其中每个项目都有标签集合。我只是对实现这一点的最有效方法感兴趣。

I have the following tables structure:

Item:

Id,         
Description 

Tags:

Id,
TagName

ItemXrefTag

 Id,
 TagId,
 ItemId

What is the best way to read all the items related to some tag and all other tags, related to the items, to be able to display list of items and all the tags related to the item?

If it's not clear I`ll give the example:

Item, Tags:

 Code complete, book|programming|cool
 Reactoring, book|programming|refactoring|cool
 C# for dummies, book|dont like it| not cool

P.S. I'm using subsonic, but as it support getting data fro mthe query Im ok with the query that will let me get all the data. Sure I can perform join and iterate through multiple lines Ill get and make collection of items with collection of tags for each of it. Im just interested in the most efficient way to implement this.

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

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

发布评论

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

评论(2

浊酒尽余欢 2024-10-05 14:55:01

您正在搜索 GROUP_CONCAT 函数。刚刚在我的数据库上尝试过,

SELECT o.orderno,
       GROUP_CONCAT(d.itemno ORDER BY d.itemno ASC SEPARATOR ', ') as items
FROM order o
LEFT JOIN order_detail d ON o.id = d.order_id
GROUP BY d.order_id
ORDER BY o.id ASC

返回订单号的结果,并以逗号分隔的订购项目列表:

orderno   | items
----------------------------------
201010001 | 100123, 100456, 100987
201010002 | 123456, 123457

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

但我想这不是 ANSI SQL,因此它不适用于 SQL-Server ;-),但是在 stackoverflow 上快速搜索返回了这个问题: 在 Microsoft SQL Server 2005 中模拟 group_concat MySQL 函数?

sql server 中相应的命令应该是 CROSS APPLY

另一篇关于交叉应用的好文章

http://www. sqlteam.com/article/using-cross-apply-in-sql-server-2005

对于亚音速的使用,您应该使用 InlineQuery (又名 CodingHorror)以 subsonic 执行原始 sql。

You are searching for the GROUP_CONCAT function. Just tried it on my database with

SELECT o.orderno,
       GROUP_CONCAT(d.itemno ORDER BY d.itemno ASC SEPARATOR ', ') as items
FROM order o
LEFT JOIN order_detail d ON o.id = d.order_id
GROUP BY d.order_id
ORDER BY o.id ASC

returns a result of order numbers with a comma seperated list of ordered items:

orderno   | items
----------------------------------
201010001 | 100123, 100456, 100987
201010002 | 123456, 123457

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

But I suppose this is not ANSI SQL so it won't be available for SQL-Server ;-), but a quick search here at stackoverflow returned this question: Simulating group_concat MySQL function in Microsoft SQL Server 2005?

The corresponding command in sql server should be CROSS APPLY

Another good article about CROSS APPLY

http://www.sqlteam.com/article/using-cross-apply-in-sql-server-2005

For usage with subsonic you should use an InlineQuery (aka CodingHorror) to execute raw sql with subsonic.

德意的啸 2024-10-05 14:55:01

这是一个连接所有数据的示例。除非您指定您想要的格式,否则这是我能做的最好的。

declare @item table (IID int identity(1,1), Description varchar(max))
declare @tags table (TID int identity(1,1), TagName varchar(50))
declare @ItemXrefTag table (XID int identity(1,1), TID int, IID int)


insert into @item values ('Book A')
insert into @item values ('Book B')


insert into @tags values ('Awesome!')
insert into @tags values ('Suckage!')
insert into @tags values ('Mediocre')


insert into @ItemXrefTag values (1,1)
insert into @ItemXrefTag values (3,1)
insert into @ItemXrefTag values (2,2)








select *
from @ItemXrefTag a
    left outer join @tags b
        on a.TID=b.TID
    left outer join @item c
        on a.IID=c.IID

here's an example to join all the data. unless you specify a format you want this is the best i can do.

declare @item table (IID int identity(1,1), Description varchar(max))
declare @tags table (TID int identity(1,1), TagName varchar(50))
declare @ItemXrefTag table (XID int identity(1,1), TID int, IID int)


insert into @item values ('Book A')
insert into @item values ('Book B')


insert into @tags values ('Awesome!')
insert into @tags values ('Suckage!')
insert into @tags values ('Mediocre')


insert into @ItemXrefTag values (1,1)
insert into @ItemXrefTag values (3,1)
insert into @ItemXrefTag values (2,2)








select *
from @ItemXrefTag a
    left outer join @tags b
        on a.TID=b.TID
    left outer join @item c
        on a.IID=c.IID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文