简体   繁体   English

如何查询has_many关联表的参数?

[英]How do I query on my has_many association's table's parameter?

Category 
String id; 
String name; 
boolean enabled;

Product 
String id; 
String name; 
boolean enabled; 
int categoryId;

I need to search for all of the enabled categories that actually have products and those products should have enabled set to true . 我需要搜索实际拥有products所有已enabled类别,并且这些products应该已enabled设置为true

I am not really that adept at SQL queries so could someone please point me to the right direction? 我不是那么擅长SQL查询所以有人可以指出我正确的方向吗?

Here it is: 这里是:

SELECT Category.* FROM Category   
INNER JOIN Product ON Category.id=Product.categoryId  
 WHERE  Category.enabled = 1 AND Product.enabled=1
 GROUP BY Category.id   

I am not sure about the boolean values representation in your DB, assuming it is 1/0 我不确定DB中的布尔值表示,假设它是1/0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 4-如何通过has_many关联在同一列上具有多个条件来查询集合? - Rails 4 - how do I query on a collection through a has_many association with multiple conditions on the same columns? 查询通过关联从 has_many 获取数据 - query to fetch data from has_many through association 具有has_many关联的联接表列分组 - group by join table column with has_many association 如何为此逻辑编写一个SQL查询(has_many,belongs_to关联)? - How to write a SQL query for this logic (has_many, belongs_to association)? 如何通过关系通过has_many缩短三联Join ActiveRecord查询? - How do I shorten a triple Joins ActiveRecord Query with a has_many through relations? 如何使用多态has_many'through'进行联接? - how do I do a join with a polymorphic has_many 'through'? has_many的反向查询怎么通过? - How can I query the reverse of has_many through? has_many与计数值的关联 - has_many association with count values 通过Rails中的关联使用has_many创建新模型时出现不允许的参数错误 - Unpermitted parameter error in creating new model with has_many through association in Rails 通过named_scope返回对象数组 - has_many ... belongs_to association; UNION ALL查询 - Returning array of objects via named_scope — has_many…belongs_to association; UNION ALL query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM