通过一次查询获取项目和所有相关标签
我有以下表格结构:
项目:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在搜索
GROUP_CONCAT
函数。刚刚在我的数据库上尝试过,返回订单号的结果,并以逗号分隔的订购项目列表:
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 withreturns a result of order numbers with a comma seperated list of ordered items:
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
(akaCodingHorror
) to execute raw sql with subsonic.这是一个连接所有数据的示例。除非您指定您想要的格式,否则这是我能做的最好的。
here's an example to join all the data. unless you specify a format you want this is the best i can do.