用rails
的都用过where
方法,总结了一些基本的,欢迎提供更多的内容。
where
方法用来指定限制获取记录的条件,用于 sql
语句的where
子句。条件可使用字符串、数组或 Hash 指定。
1.String纯字符串条件
>> Applicant.where("name = '夏女士'")
=> #SELECT `applicants`.* FROM `applicants` WHERE (name = '夏女士');
2.Array数组
对于?
号,Active Record
会将先处理第一个元素中的条件,然后使用后续元素替换第一个元素中的问号?
,问号个数与后续元素个数相同。
>> Applicant.where(["name = ? and sex = ?", "夏女士", 1]) 或
>> Applicant.where("name = ? and sex = ?", "夏女士", 1)
=> #SELECT `applicants`.* FROM `applicants` WHERE (name = '夏女士' and sex = 1)
除了使用问号占位之外,在数组条件中还可使用键值对 Hash
形式的占位符
>> Applicant.where(["name = :name and sex = :sex", { name: "夏女士", sex: 1}]) 或
>> Applicant.where("name = :name and sex = :sex", { name: "夏女士", sex: 1 })
=> #SELECT `applicants`.* FROM `applicants` WHERE (name = '夏女士' and sex = 1)
还能这样:
>> Applicant.where(["name = '%s' and sex = '%s'", "夏女士", 1]) 或
>> Applicant.where("name = '%s' and sex = %s", "夏女士", 1)
=> #SELECT `applicants`.* FROM `applicants` WHERE (name = '夏女士' and sex = 1)
3.Hash
下面最外层的{}
符号是可以去掉的
>> Applicant.where({ name: "夏女士", sex: 1 }) 可以写成
>> Applicant.where(name: "夏女士", sex: 1)
=> #SELECT `applicants`.* FROM `applicants` WHERE `applicants`.`name` = '夏女士' AND `applicants`.`sex` = 1
<b>子集查询</b>:
>> Applicant.where(name: ["夏女士", "张女士"])
=> #SELECT `applicants`.* FROM `applicants` WHERE `applicants`.`name` IN ('夏女士', '张女士')
查询name
是夏女士
和张女士
的记录,使用的是in
子句查询记录
<b>范围查询</b>:
>> Applicant.where(id: 1..5)
=> #SELECT `applicants`.* FROM `applicants` WHERE (`applicants`.`id` BETWEEN 1 AND 5)
查询id
在1到5之间的值,用的sql between
查询,区别于[1,2,3,4,5]
。更常用于时间段查询。
字段的名字还可使用字符串表示:
>> Applicant.where('sex' => 1)
=> #SELECT `applicants`.* FROM `applicants` WHERE `applicants`.`sex` = 1
4.Joins关联
下面是两个model
的关系
class Applicant < ActiveRecord::Base
has_many :companies
end
class Company < ActiveRecord::Base
belongs_to :applicant
#<Company id: 3, applicant_id: 1, name: "阿里巴巴", type_id: 1>
end
通过关联查询companies
表中type_id
为1的所有applicants
记录:
Applicant.joins(:companies).where("companies.type_id = ?", 1)
#SELECT `applicants`.* FROM `applicants` INNER JOIN `companies` ON `companies`.`applicant_id` = `applicants`.`id` WHERE (companies.type_id = 1)
或者这样:
>> Applicant.joins(:companies).where(companies: { type_id: 1 })
=> #SELECT `applicants`.* FROM `applicants` INNER JOIN `companies` ON `companies`.`applicant_id` = `applicants`.`id` WHERE `companies`.`type_id` = 1
下面的方法是不行的:
>> Applicant.joins(:companies).where(type_id: 1)
=> #Mysql2::Error: Unknown column 'applicants.type_id' in 'where clause'
5.where.not用法
sql not
查询可用where.not
方法构建。
>> Applicant.where.not(sex: 1)
=> #SELECT `applicants`.* FROM `applicants` WHERE (`applicants`.`sex` != 1)
如果还想用where
查询其他字段:
>> Applicant.where(name: '夏女士').where.not(sex: 1)
=> #SELECT `applicants`.* FROM `applicants` WHERE `applicants`.`name` = '夏女士' AND (`applicants`.`sex` != 1)
关联查询companies
表中type_id
不为空的记录:
>> Applicant.includes(:companies).where.not(companies: {type_id: nil})
=> #SELECT `applicants`.* FROM `applicants` LEFT OUTER JOIN `companies` ON `companies`.`applicant_id` = `applicants`.`id` WHERE (`companies`.`type_id` IS NOT NULL)
欢迎指正、补充!