如何获取剩余记录的特定记录?

时间:2015-10-20 23:28:17

标签: sql ruby-on-rails activerecord rails-activerecord

让我们说我有一些记录,而且我希望所有的汽车都能在 type =" honda"和门=" 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 =&#34; honda&#34;和门=&#34; 4&#34; 主要是我不想列出IS NOT的其他类型,因为我不确定他们的价值观。

Car.where("type = ? AND doors = ?", "honda", "4")

1 个答案:

答案 0 :(得分:1)

使用OR运算符选择“honda”

以外的其他类型
Car.where("(type = ? AND doors = ?) OR type <> ?", "honda", "4", "honda")
相关问题