假设我有一些记录,例如,我想获取所有类型为“ honda”和doors =“ 4”的汽车,同时保留所有剩余记录
<Car id="1", type="honda", doors="4" created_at: "2015.01.01">
<Car id="2", type="jeep", created_at: "2015.01.01">
<Car id="3", type="mazda", created_at: "2015.01.01">
<Car id="4", type="honda", doors="4" created_at: "2015.01.01">
<Car id="5", type="honda", doors="2" created_at: "2015.01.01">
<Car id="6", type="honda", doors="2" created_at: "2015.01.01">
我想得到这个:
<Car id="1", type="honda", doors="4" created_at: "2015.01.01">
<Car id="2", type="jeep", created_at: "2015.01.01">
<Car id="3", type="mazda", created_at: "2015.01.01">
<Car id="4", type="honda", doors="4" created_at: "2015.01.01">
我有这个查询,但是它只返回type =“ honda”和doors =“ 4”的汽车,主要是我不想列出其他类型,IS NOT
因为我不确定它们的值。
Car.where("type = ? AND doors = ?", "honda", "4")
使用OR
运算符选择“本田”以外的其他类型
Car.where("(type = ? AND doors = ?) OR type <> ?", "honda", "4", "honda")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句