选择在规范化表中具有多个匹配项的项目

发布于 2024-11-02 07:43:30 字数 704 浏览 0 评论 0原文

我有一个存储一堆对象的表。每个对象可以有多种颜色,这些颜色存储在由 object_id 连接的规范化表中。

如果我去

SELECT `object_name` FROM `objects`
  LEFT JOIN `object_color` USING `object_id`
    WHERE `object_color` IN ('red', 'blue');

,那么我会得到“红色”OR“蓝色”的对象。我需要获取所有“红色”“蓝色”的对象。如果我去:

SELECT `object_name` FROM `objects`
  LEFT JOIN `object_color` USING `object_id`
    WHERE `object_color` = 'red' AND `object_color` = 'blue';

那么我什么也得不到,因为每一行中只有一个 object_color 并且不能同时是两者。另外,实际上,颜色是另一个表中带有名称的 id。为了这个问题,我简化了这里的一切。

我需要能够搜索无限数量的颜色。

谢谢

编辑:

object_color仅在object_color表中。

任何物体都会有任何单一的颜色。

I have a table that stores a bunch of objects. Each object can have many colors which are stored in a normalized table connected by the object_id.

If I go

SELECT `object_name` FROM `objects`
  LEFT JOIN `object_color` USING `object_id`
    WHERE `object_color` IN ('red', 'blue');

Then I get objects that are 'red' OR 'blue'. I need to get all objects that are 'red' AND 'blue'. If I go:

SELECT `object_name` FROM `objects`
  LEFT JOIN `object_color` USING `object_id`
    WHERE `object_color` = 'red' AND `object_color` = 'blue';

Then I get nothing as there's only one object_color in each line and it can't be both. Also, in actuality, the colors are id's with names in another table. I simplified everything here for the sake of the question.

I need to be able to search for an unlimited number of colors.

Thanks

EDIT:

object_color is only in the object_color table.

And any object will have any single color once.

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

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

发布评论

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

评论(4

尸血腥色 2024-11-09 07:43:30
Select object_name
From objects
Where object_color In('red','blue')
Group By object_name
Having Count(Distinct object_color) = 2

顺便说一句,您从未提及 object_color 列源自哪个表。如果它来自 object_color 表:

Select O.object_name
From objects As O
    Join object_color As C
        On C.object_id = O.object_id
Where C.object_color In('red','blue')
Group By O.object_name
Having Count(Distinct C.object_color) = 2

上述查询假设给定的 object 行不能有多个 object_color 行相同的颜色。但是,正如 Joel C 指出的那样,如果一个 object 可能具有多个红色或蓝色的 object_color 行,则需要不同的查询。 :

Select ...
From objects As O
Where O.object_id In    (
                        Select C1.object_id
                        From object_color As C1
                        Where C1.object_color = 'red'
                        )
    And O.object_id In  (
                        Select C1.object_id
                        From object_color As C1
                        Where C1.object_color = 'blue'
                        )

还有一个解决方案:

Select O.object_name
From objects As O
    Join    (
            Select C1.object_id, C1.object_color
            From object_color As C1
            Where C1.color In('red','blue')
            Group By C1.object_id, C1.object_color
            ) As Z
        On Z.object_id = O.object_id
Group By O.object_name
Having Count(*) = 2
Select object_name
From objects
Where object_color In('red','blue')
Group By object_name
Having Count(Distinct object_color) = 2

Btw, you never mention from which table the object_color column derives. If it is from the object_color table:

Select O.object_name
From objects As O
    Join object_color As C
        On C.object_id = O.object_id
Where C.object_color In('red','blue')
Group By O.object_name
Having Count(Distinct C.object_color) = 2

The above query assumes that a given object row could have not have multiple object_color rows of the same color. However, as Joel C noted, if it were possible for an object to have multiple object_color rows of red or blue, then that requires a different query. :

Select ...
From objects As O
Where O.object_id In    (
                        Select C1.object_id
                        From object_color As C1
                        Where C1.object_color = 'red'
                        )
    And O.object_id In  (
                        Select C1.object_id
                        From object_color As C1
                        Where C1.object_color = 'blue'
                        )

Yet another solution:

Select O.object_name
From objects As O
    Join    (
            Select C1.object_id, C1.object_color
            From object_color As C1
            Where C1.color In('red','blue')
            Group By C1.object_id, C1.object_color
            ) As Z
        On Z.object_id = O.object_id
Group By O.object_name
Having Count(*) = 2
悲喜皆因你 2024-11-09 07:43:30

我更喜欢 ON 而不是 USING

SELECT o.object_name
FROM objects o
  JOIN object_color oc
    ON o.object_id = oc.object_id
WHERE oc.object_color IN ( 'red', 'blue' )
GROUP BY o.object_id
HAVING COUNT(o.object_id) = ( SELECT COUNT(*)
                              FROM ( 'red', 'blue' )
                            )

假设一个对象不可能有许多具有相同颜色的行。

I prefer ON rather than USING:

SELECT o.object_name
FROM objects o
  JOIN object_color oc
    ON o.object_id = oc.object_id
WHERE oc.object_color IN ( 'red', 'blue' )
GROUP BY o.object_id
HAVING COUNT(o.object_id) = ( SELECT COUNT(*)
                              FROM ( 'red', 'blue' )
                            )

Assuming that it is not possible for an object to have many rows with same colour.

三岁铭 2024-11-09 07:43:30

您需要多重加入

SELECT `object_name` FROM `objects`
  LEFT JOIN `object_color` USING `object_id`
    WHERE `object_color` = 'red'
  LEFT JOIN `object_color` USING `object_id`
    WHERE `object_color` = 'blue' ;

You'll need a multi join

SELECT `object_name` FROM `objects`
  LEFT JOIN `object_color` USING `object_id`
    WHERE `object_color` = 'red'
  LEFT JOIN `object_color` USING `object_id`
    WHERE `object_color` = 'blue' ;
花开半夏魅人心 2024-11-09 07:43:30

如果您使用的 SQL 支持,您也可以使用 INTERSECT。

SELECT object_name
FROM objects o, objects_color oc
WHERE object_color = 'red'
and o.object_id = oc.object_id

INTERSECT

SELECT object_name
FROM objects o, objects_color oc
WHERE object_color = 'blue'
and o.object_id = oc.object_id

这将使两个表相交,并且仅显示具有与相同对象名称匹配的红色和蓝色的行。

You can also use INTERSECT if it is supported in the SQL you're using.

SELECT object_name
FROM objects o, objects_color oc
WHERE object_color = 'red'
and o.object_id = oc.object_id

INTERSECT

SELECT object_name
FROM objects o, objects_color oc
WHERE object_color = 'blue'
and o.object_id = oc.object_id

This will intersect the two tables and only show rows that have both red and blue color matched with the same object name.

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