我在数据库中有 3 个表dishes,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
预期结果:
然后,我必须从表中获取idandnamedishesid7,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))