mysql AND 子句在同一列上多次 [英] mysql AND clause on same column multiple times

查看:64
本文介绍了mysql AND 子句在同一列上多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检索具有多种便利设施的房产.

I am trying to retrieve properties which has multiple amenities.

但表结构是:属性在property表中,设施与属性相关联.

But the table structure is: properties are in property table and amenities are associated with the property.

设施表有:

id property_id amenity_name ....

现在我想搜索有停车场"和电梯"等的便利设施.

Now i want to search the amenities that have 'parking' and 'elevator' and so on.

select property_id from amenities where name = 'parking' and name = 'elevator';

我不想为此触发连接查询.

I dont want to fire a join query for this.

那么在这种情况下,谁能救救我?

So can anybody save me in this situation?

推荐答案

试试这个 -:

   scope :amenity_type, lambda {|term|
          where("amenities.name" => term).joins(:amenities).group("amenities.property_id").having("count(amenities.name) >= #{term.size}") unless term.blank?
   }

这篇关于mysql AND 子句在同一列上多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆