データベース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
テーブルからid
andを取得する必要があります。ID が含まれていない場合と ID が含まれていない場合、IDが重複していません。name
dishes
price_deals
give_away_deals
dishes
テーブルid
1 から 10 に10 個のレコードがあるとします。
price_deals
表にはdish_id
があり2,4,5
ます。
give_away_deals
表にdish_id
は1,3,6
期待される結果:
次に、テーブル where areからid
andを取得する必要がありますname
dishes
id
7,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))