用于选择项目/属性列表中具有多个属性的项目的 SQL 语句是什么?

发布于 2024-07-23 07:52:53 字数 203 浏览 5 评论 0原文

假设我有一个表,其中列出了项目和属性,例如,

frog    green
cat     furry
frog    nice
cat     4 legs
frog    4 legs

我想从项目列中选择同时具有绿色和 4 条腿属性的唯一对象。 在这种情况下,我希望只返回青蛙对象。 执行此操作最有效的查询是什么?

Say I have a table that has items and attributes listed like,

frog    green
cat     furry
frog    nice
cat     4 legs
frog    4 legs

From the items column I want to select unique objects that have both the green and 4 legs attribute. I would expect to get back just the frog object in this case. What is the most efficient query to do this?

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

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

发布评论

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

评论(9

め七分饶幸 2024-07-30 07:52:53
select  item.name 
from    item 
where   item.attribute in ('4 legs', 'green') 
group by item.name 
having  count(distinct item.attribute) = 2
select  item.name 
from    item 
where   item.attribute in ('4 legs', 'green') 
group by item.name 
having  count(distinct item.attribute) = 2
耳钉梦 2024-07-30 07:52:53

最有效的方法是使用自连接:

SELECT * FROM attributes a1 
JOIN attributes a2 USING (item_name) -- e.g. frog
WHERE a1.value = 'green' AND a2.value = '4 legs';

有些人使用的另一种解决方案是 GROUP BY 的技巧:

SELECT item_name FROM attributes
WHERE value IN ('4 legs', 'green')
GROUP BY item_name
HAVING COUNT(*) = 2;

但 GROUP BY 解决方案可能不如 JOIN 高效,具体取决于您使用的 RDBMS 品牌。 此外,随着表中容量的增加,一种方法可能会更好地扩展。

The most efficient way to do this is with a self-join:

SELECT * FROM attributes a1 
JOIN attributes a2 USING (item_name) -- e.g. frog
WHERE a1.value = 'green' AND a2.value = '4 legs';

Another solution that some people use is a trick with GROUP BY:

SELECT item_name FROM attributes
WHERE value IN ('4 legs', 'green')
GROUP BY item_name
HAVING COUNT(*) = 2;

But the GROUP BY solution may not be as efficient as a JOIN, depending on which brand of RDBMS you use. Also one method may scale better as the volume in your table grows.

负佳期 2024-07-30 07:52:53

select * from table where thing='frog'

没有什么比确切地知道你想要什么更好的了。

select * from table where thing='frog'

nothing beats knowing exatcly what you want.

百思不得你姐 2024-07-30 07:52:53
select
    item, count(*)
from
    @temp
where
    attribute in ('4 legs','green')
group by
    item
having
    count(*) = 2 -- this "2" needs to be replaced with however many attributes you have
select
    item, count(*)
from
    @temp
where
    attribute in ('4 legs','green')
group by
    item
having
    count(*) = 2 -- this "2" needs to be replaced with however many attributes you have
困倦 2024-07-30 07:52:53

您还可以单独查询每个属性,然后将它们相交......

/*
-- create sample table...
create table #temp1
    (item varchar(max),
    attrib varchar(max))

-- populate sample table (SQL 08)...
insert #temp1
values ('frog', 'green'), ('cat', 'furry'), ('frog', 'nice'), ('cat', '4 legs'), ('frog', '4 legs')
*/


SELECT  item
FROM    #temp1
WHERE   attrib = 'green'
INTERSECT
SELECT  item
FROM    #temp1
WHERE   attrib = '4 legs'

You could also query each attribute separately, and then intersect them...

/*
-- create sample table...
create table #temp1
    (item varchar(max),
    attrib varchar(max))

-- populate sample table (SQL 08)...
insert #temp1
values ('frog', 'green'), ('cat', 'furry'), ('frog', 'nice'), ('cat', '4 legs'), ('frog', '4 legs')
*/


SELECT  item
FROM    #temp1
WHERE   attrib = 'green'
INTERSECT
SELECT  item
FROM    #temp1
WHERE   attrib = '4 legs'
百合的盛世恋 2024-07-30 07:52:53

创建两张表,一张是项目表,一张是属性表。
项目可以是名称、intAttributeID,其中intAttributeID 是对属性表的外键引用。 这样您就可以根据您关心的内容执行选择语句。

create two tables, one of items and one of attributes.
Items could be name, intAttributeID, where intAttributeID is a foreign key reference to the Attributes table. That way you can do a select statement based off whatever you care about.

青春有你 2024-07-30 07:52:53

但也许这可以帮助你:

SELECT * 
FROM tbl t1
INNER JOIN tbl t2 ON t1.Name = t2.Name
WHERE t1.Attribute = 'green' AND t2.Attribute = '4 legs'

But maybe this can help you:

SELECT * 
FROM tbl t1
INNER JOIN tbl t2 ON t1.Name = t2.Name
WHERE t1.Attribute = 'green' AND t2.Attribute = '4 legs'
还给你自由 2024-07-30 07:52:53

很难,因为它不是标准化模型。 这是一个周末。

您要过滤多个未连接的行,因此您必须依次提取每个属性,然后匹配项目。

SELECT
   item
FROM
    (SELECT
        item
    FROM
        Mytable
    WHERE
        attribute = '4 legs') k1
    JOIN
    (SELECT
        item
    FROM
        Mytable
    WHERE
        attribute = 'green') k2 ON k1.item = k2.item

Hard because it's not a normalised model. It's a weekend.

You are filtering across multiple, unconnected rows, so you'd have to extract each attribute in turn and then match items.

SELECT
   item
FROM
    (SELECT
        item
    FROM
        Mytable
    WHERE
        attribute = '4 legs') k1
    JOIN
    (SELECT
        item
    FROM
        Mytable
    WHERE
        attribute = 'green') k2 ON k1.item = k2.item
故事还在继续 2024-07-30 07:52:53

如果可以的话,我会重新设计。 这不是您能够同时有效查询 12 个值的方法(它需要 12 个连接)

请阅读这篇维基百科文章
http://en.wikipedia.org/wiki/Entity-Attribute-Value_model#缺点

还没有见过使用这种模型的数据库最终不会遇到严重的性能问题。 这种设计对于非数据库人员来说看起来很优雅,但实际上通常是数据库设计不当的标志。

If possible, I would redesign. This is not something you will ever be able to effectively query 12 values on at the same time on (it will require 12 joins)

Please read this wikipedia article
http://en.wikipedia.org/wiki/Entity-Attribute-Value_model#Downsides

Never seen a database yet that used this model that didn't run into serious performance issues eventually. This design looks elegant to non-database people but is actually usually a sign of a badly designed database.

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