如何搜索“财产包” SQL 中的表?

发布于 2024-07-11 09:44:15 字数 770 浏览 4 评论 0原文

我有一个基本的“属性包”表,用于存储有关我的主表“卡”的属性。 因此,当我想开始对卡片进行一些高级搜索时,我可以这样做:

SELECT dbo.Card.Id, dbo.Card.Name
  FROM dbo.Card
         INNER JOIN dbo.CardProperty ON dbo.CardProperty.IdCrd = dbo.Card.Id
 WHERE dbo.CardProperty.IdPrp = 3 AND dbo.CardProperty.Value = 'Fiend'
INTERSECT
  SELECT dbo.Card.Id, dbo.Card.Name
  FROM dbo.Card
         INNER JOIN dbo.CardProperty ON dbo.CardProperty.IdCrd = dbo.Card.Id
 WHERE (dbo.CardProperty.IdPrp = 10 AND (dbo.CardProperty.Value = 'Wind' OR dbo.CardProperty.Value = 'Fire'))

我需要做的是将这个想法提取到某种存储过程中,以便理想情况下我可以传入属性/值列表组合并得到搜索结果。

最初,这将是一个“严格”搜索,意味着结果必须与查询中的所有元素匹配,但我也希望有一个“宽松”查询,以便它与查询中的任何结果匹配。

我似乎不太明白这个问题。 我之前的版本是生成一些大量的 SQL 查询来执行,其中包含大量 AND/OR 子句,但我希望这次做一些更优雅的事情。 我该怎么做呢?

I have a basic "property bag" table that stores attributes about my primary table "Card." So when I want to start doing some advanced searching for cards, I can do something like this:

SELECT dbo.Card.Id, dbo.Card.Name
  FROM dbo.Card
         INNER JOIN dbo.CardProperty ON dbo.CardProperty.IdCrd = dbo.Card.Id
 WHERE dbo.CardProperty.IdPrp = 3 AND dbo.CardProperty.Value = 'Fiend'
INTERSECT
  SELECT dbo.Card.Id, dbo.Card.Name
  FROM dbo.Card
         INNER JOIN dbo.CardProperty ON dbo.CardProperty.IdCrd = dbo.Card.Id
 WHERE (dbo.CardProperty.IdPrp = 10 AND (dbo.CardProperty.Value = 'Wind' OR dbo.CardProperty.Value = 'Fire'))

What I need to do is to extract this idea into some kind of stored procedure, so that ideally I can pass in a list of property/value combinations and get the results of the search.

Initially this is going to be a "strict" search meaning that the results must match all elements in the query, but I'd also like to have a "loose" query so that it would match any of the results in the query.

I can't quite seem to wrap my head around this one. My previous version of this was to do generate some massive SQL query to execute with a lot of AND/OR clauses in it, but I'm hoping to do something a little more elegant this time. How do I go about doing this?

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

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

发布评论

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

评论(1

人│生佛魔见 2024-07-18 09:44:15

在我看来,您在这里有一个 EAV 模型。
如果您使用的是 sql server 2005 及更高版本,我建议您使用 XML 数据类型:

http://weblogs.sqlteam.com/mladenp/archive/2006/10/14/14032.aspx

通过内置的 xml 查询功能使搜索和内容变得更加容易。

如果您无法更改模型,请查看以下内容:

http://weblogs.sqlteam .com/davidm/articles/12117.aspx

it seems to me that you have an EAV model here.
if you're using sql server 2005 and up i'd suggest you use XML datatype for this:

http://weblogs.sqlteam.com/mladenp/archive/2006/10/14/14032.aspx

makes searching and stuff much easier with built in xml querying capabilities.

if you can't change your model then look at this:

http://weblogs.sqlteam.com/davidm/articles/12117.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文