データベースdishesに3 つのテーブルがprice_dealsあり、give_away_deals
# == Schema Information
#
# Table name: dishes
#
# id :integer not null, primary key
# name :string(255)
# created_at :datetime
# updated_at :datetime
# Table name: price_deals
#
# id :integer not null, primary key
# name :string(255)
# dish_id :integer
# created_at :datetime
# updated_at :datetime
# Table name: give_away_deals
#
# id :integer not null, primary key
# name :string(255)
# dish_id :integer
# created_at :datetime
# updated_at :datetime
テーブルからidandを取得する必要があります。ID が含まれていない場合と ID が含まれていない場合、IDが重複していません。namedishesprice_dealsgive_away_deals
dishesテーブルid1 から 10 に10 個のレコードがあるとします。
price_deals表にはdish_idがあり2,4,5ます。
give_away_deals表にdish_idは1,3,6
期待される結果:
次に、テーブル where areからidandを取得する必要がありますnamedishesid7,8,9,10
このクエリを試したところ、
Dish.all(:select => "id, name", :conditions => ["id not in (select dish_id from price_deals)", "id not in (select dish_id from give_away_deals)"])
ただし、 にないデータのみを返しますprice_deals。
上記のクエリの何が問題で、期待される結果を得るにはどうすればよいですか?
これは、Rails サーバーで取得した SQL クエリです。
SELECT id, name FROM "dishes" WHERE (id not in (select dish_id from price_deals))