Mysql问题:有没有类似IN ALL的查询?
例如这个查询:
SELECT `variants`.*
FROM `variants` INNER JOIN `variant_attributes`
ON variant_attributes.variant_id = variants.id
WHERE (variant_attributes.id IN ('2','5'))
Andvarianthas_manyvariant_attributes
我真正想做的是找到哪个变体同时具有 ID = 2 和 5 的两个变体属性。这对于 MySQL 来说可能吗?额外的问题,有没有一种快速的方法可以使用 Ruby on Rails(也许使用 SearchLogic)来做到这一点?
解决方案
谢谢Quassnoi 提供的查询,效果非常好。
为了在Rails上使用,我使用了下面的named_scope,我认为这对于初学者来说更容易理解。
基本上,named_scope 会返回 {:from =>; x,:条件=> y} 和上面的行用于设置 y 变量。
named_scope :with_variant_attribute_values, lambda { |values|
conditions = ["(
SELECT COUNT(*)
FROM `variant_attributes`
WHERE variant_attributes.variant_id = variants.id
AND variant_attributes.value IN (#{values.collect { |value| "?" }.join ", "})
) = ?
"]
conditions = conditions + values + [values.length]
{
:from => 'variants',
:conditions => conditions
}}
For example this query:
SELECT `variants`.*
FROM `variants` INNER JOIN `variant_attributes`
ON variant_attributes.variant_id = variants.id
WHERE (variant_attributes.id IN ('2','5'))
And variant has_many variant_attributes
What I actually want to do is to find which variant has BOTH variant attributes with ID = 2 and 5. Is this possible with MySQL? Bonus Question, is there a quick way to do this with Ruby on Rails, perhaps with SearchLogic?
solution
Thank you Quassnoi for the query you provided, that worked perfectly.
To use on Rails, I used the named_scope below, I think this is simpler to understand for beginners.
Basically named_scope would return {:from => x, :conditions => y} and the lines above were used to setup the y variable.
named_scope :with_variant_attribute_values, lambda { |values|
conditions = ["(
SELECT COUNT(*)
FROM `variant_attributes`
WHERE variant_attributes.variant_id = variants.id
AND variant_attributes.value IN (#{values.collect { |value| "?" }.join ", "})
) = ?
"]
conditions = conditions + values + [values.length]
{
:from => 'variants',
:conditions => conditions
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设
variant_attributes (variant_id, id)
是唯一的:Assiuming that
variant_attributes (variant_id, id)
is unique:Quassnoi 发布了 mysql 查询,可以完成您想要的操作。下面是用于
Variant
模型的方法,它可以执行相同的操作。我正在采用两种方法,一种是如果variant_attributes (variant_id, id)
是唯一的组合,另一种是如果它们不是唯一的:
非唯一
可以通过以下方式使用:
此代码没有没有经过测试。
Quassnoi has posted the mysql query that does what you would like. Here is a method for the
Variant
model that will do the equivalent. I'm doing two approaches, one ifvariant_attributes (variant_id, id)
are a unique combination, and one if they aren'tUnique:
Non Unique
Which can be used in the following ways:
This code hasn't been tested.
我将为此创建一个命名范围:
现在您可以使用命名范围,如下所示:
I would create a named_scope for this:
Now you can use the named scope as follows:
感谢 Quassnoi 提供的查询,效果非常好。
为了在Rails上使用,我使用了下面的named_scope,我认为这对于初学者来说更容易理解。
基本上,named_scope 会返回 {:from =>; x,:条件=> y} 和上面的行用于设置 y 变量。
Thank you Quassnoi for the query you provided, that worked perfectly.
To use on Rails, I used the named_scope below, I think this is simpler to understand for beginners.
Basically named_scope would return {:from => x, :conditions => y} and the lines above were used to setup the y variable.