Mysql交集中的单列多过滤器

发布于 2024-08-29 13:42:04 字数 492 浏览 1 评论 0原文

这是一个表格

CarID| Attribute    | Value
1   | Color     | Red
2   | Color     | Blue 
3   | Color     | Red 
1   | Type      | Coupe 
2   | Type      | Hatch Back 
3   | Type      | Coupe
3   | Make      | Honda
2   | Make      | Toyota
1   | Make      | Ford

现在我想运行一个过滤器,例如 Select * From Cars WHERE (Attribute = Color AND Value = Red) AND (Attribute = Make AND Value = Honda).... 并希望获得 CarID 为 3 !

这是两个查询的交集的简单情况,但我不知道如何在单个查询中完成它。

任何帮助都适用。

Here is a table

CarID| Attribute    | Value
1   | Color     | Red
2   | Color     | Blue 
3   | Color     | Red 
1   | Type      | Coupe 
2   | Type      | Hatch Back 
3   | Type      | Coupe
3   | Make      | Honda
2   | Make      | Toyota
1   | Make      | Ford

Now I would like to run a filter Like Select * From Cars WHERE (Attribute = Color AND Value = Red) AND (Attribute = Make AND Value = Honda).... and Hope to get the CarID as 3 !

This is simple case of Intersection of 2 queries but I don't know how to get it done in a single query.

Any help appriciated.

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

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

发布评论

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

评论(3

心房敞 2024-09-05 13:42:04
select
      carid,
      count(*) matchedItems
   from
      YourTable
   where
        ( Attribute = 'Color' and Value = 'Red' )
     OR ( Attribute = 'Make' and Value = 'Honda' )
   group by 
      carid 
   having
      matchedItems = 2;

你可能不得不改变...

 having count(*) = 2
select
      carid,
      count(*) matchedItems
   from
      YourTable
   where
        ( Attribute = 'Color' and Value = 'Red' )
     OR ( Attribute = 'Make' and Value = 'Honda' )
   group by 
      carid 
   having
      matchedItems = 2;

you may have to change the having to...

 having count(*) = 2
蝶…霜飞 2024-09-05 13:42:04

最直接的解决方案是使用子查询为每个属性模拟一个单独的表(尽管它可能不是最有效的):

SELECT Colors.CarID FROM
    ( SELECT CarID, Value FROM Cars WHERE Attribute = 'Color' ) Colors,
    ( SELECT CarID, Value FROM Cars WHERE Attribute = 'Make' ) Makes
    WHERE Colors.CarID = Makes.CarID AND Colors.Value = 'Red' AND
        Makes.Value = 'Honda';

The most straightforward solution would be to simulate a separate table for each attribute with sub queries (although it might not be the most efficient):

SELECT Colors.CarID FROM
    ( SELECT CarID, Value FROM Cars WHERE Attribute = 'Color' ) Colors,
    ( SELECT CarID, Value FROM Cars WHERE Attribute = 'Make' ) Makes
    WHERE Colors.CarID = Makes.CarID AND Colors.Value = 'Red' AND
        Makes.Value = 'Honda';
倦话 2024-09-05 13:42:04

这是查询的另一个变体

SELECT carid
FROM AnotherUnknownTableName t INNER JOIN  
     AnotherUnknownTableName t2 ON t.carid = t2.carid AND 
                                   t.Attribute = 'Color' AND 
                                   t2.Attribute = 'Make'
WHERE 
     t.Value = 'Red' AND t2.Value = 'Honda'

在 (carid, Attribute) 上建立索引将会创造奇迹。

Here is another variant of the query

SELECT carid
FROM AnotherUnknownTableName t INNER JOIN  
     AnotherUnknownTableName t2 ON t.carid = t2.carid AND 
                                   t.Attribute = 'Color' AND 
                                   t2.Attribute = 'Make'
WHERE 
     t.Value = 'Red' AND t2.Value = 'Honda'

Having an index on (carid, Attribute) will do wonders.

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