Rails 子查询加入
Rails Subquery join
我有一个应用程序需要在数据库中获取一些过滤器。过滤器是一组 A
、B
和 C
,即:
A = 1 and B = 2 and C = 3
每个过滤器都有一个值、一个特征和一个类别(由 A
、B
和 C
表示),即:
category = 'dimension' and characteristic = 'width' and value = 10
category = 'color' and characteristic = 'background' and value = 'red'
为此,数据有点太复杂了 post 但我们可以假设过滤器总是三重奏:
Filter :
category
characteristic
value
我需要从数据库中获取一定数量的过滤器。由于过滤器应用于产品,过滤器越多,我必须找到的产品就越少。
我的第一个方法是做类似的事情:
(A = 1 and B = 2 and C = 3) or
(A = 4 and B = 5 and C = 6)
但是在前面的条件下,结果是扩展的,没有限制的(过滤器必须相加)。
一个新的尝试给了我:
(A = 1 and B = 2 and C = 3) and
(A = 4 and B = 5 and C = 6)
但这显然return没什么,因为查询尝试A = 1 and A = 4
。
这里有一张图来表达我搜索的内容:
当我尝试使用 or
进行查询时,我得到了所有 (1)
和 (2)
。
select * from products where ( category = 'dimension' and characteristic = 'width' and value = 10 ) or ( category = 'color' and characteristic = 'background' and value = 'red' )
当我尝试使用 and
进行查询时,我得到 (1)
的 none 和 (2)
.
select * from products where ( category = 'dimension' and characteristic = 'width' and value = 10 ) and ( category = 'color' and characteristic = 'background' and value = 'red' )
所以一个解决方案是做一些查询然后加入它们:
select where (A = 1 and B = 2 and C = 3) (1)
select where (A = 4 and B = 5 and C = 6) (2)
select (1) join (2)
这样我得到了所有 (1)
和 (2)
但我只保留了在两个子查询中找到的产品。
但是使用这个我认为可行的解决方案,我无法使用活动记录构建此类查询。两个主要问题是:
- 我事先不知道过滤器的数量
- 对于这种情况,我还没有找到连接子查询的正确方法
你有什么idea/link推荐吗?
谢谢,
我发现,使用 MySQL 数据库,我可以简单地执行 'OR' 条件,然后仅过滤重复条目,如下所示:
SELECT * FROM products WHERE
(category = 'dimension' AND characteristic = 'width' AND value = 10 )
OR (category = 'color' AND characteristic = 'background' AND value = 'red')
GROUP BY id
HAVING count(*) > 0
我有一个应用程序需要在数据库中获取一些过滤器。过滤器是一组 A
、B
和 C
,即:
A = 1 and B = 2 and C = 3
每个过滤器都有一个值、一个特征和一个类别(由 A
、B
和 C
表示),即:
category = 'dimension' and characteristic = 'width' and value = 10
category = 'color' and characteristic = 'background' and value = 'red'
为此,数据有点太复杂了 post 但我们可以假设过滤器总是三重奏:
Filter :
category
characteristic
value
我需要从数据库中获取一定数量的过滤器。由于过滤器应用于产品,过滤器越多,我必须找到的产品就越少。
我的第一个方法是做类似的事情:
(A = 1 and B = 2 and C = 3) or
(A = 4 and B = 5 and C = 6)
但是在前面的条件下,结果是扩展的,没有限制的(过滤器必须相加)。
一个新的尝试给了我:
(A = 1 and B = 2 and C = 3) and
(A = 4 and B = 5 and C = 6)
但这显然return没什么,因为查询尝试A = 1 and A = 4
。
这里有一张图来表达我搜索的内容:
当我尝试使用 or
进行查询时,我得到了所有 (1)
和 (2)
。
select * from products where ( category = 'dimension' and characteristic = 'width' and value = 10 ) or ( category = 'color' and characteristic = 'background' and value = 'red' )
当我尝试使用 and
进行查询时,我得到 (1)
的 none 和 (2)
.
select * from products where ( category = 'dimension' and characteristic = 'width' and value = 10 ) and ( category = 'color' and characteristic = 'background' and value = 'red' )
所以一个解决方案是做一些查询然后加入它们:
select where (A = 1 and B = 2 and C = 3) (1)
select where (A = 4 and B = 5 and C = 6) (2)
select (1) join (2)
这样我得到了所有 (1)
和 (2)
但我只保留了在两个子查询中找到的产品。
但是使用这个我认为可行的解决方案,我无法使用活动记录构建此类查询。两个主要问题是:
- 我事先不知道过滤器的数量
- 对于这种情况,我还没有找到连接子查询的正确方法
你有什么idea/link推荐吗?
谢谢,
我发现,使用 MySQL 数据库,我可以简单地执行 'OR' 条件,然后仅过滤重复条目,如下所示:
SELECT * FROM products WHERE
(category = 'dimension' AND characteristic = 'width' AND value = 10 )
OR (category = 'color' AND characteristic = 'background' AND value = 'red')
GROUP BY id
HAVING count(*) > 0