显示Oracle 11g中多个表的数据

时间:2015-07-08 21:20:33

标签: sql oracle11g subquery

问题

我想显示来自不同表格的DATA组合。在这种情况下,食品和饮料的所有组合,到目前为止尚未订购。

我的数据库表

CUSTOMER_ORDER

Orderno(PK)  DateOrd    DateReq    Address
O00001       03-Apr-11  07-Apr-11  Union St
O00002       05-Apr-11  01-May-11  St. Andrew St.
O00003       12-Apr-11  27-Apr-11  Garthdee
O00004       12-Apr-11  17-Apr-11  Union St.

Dishid(PK)  DishName         Vegetarian   Price
D0001       Pasta bake       yes           6.00
D0002       Fish pie         No            9.00
D0003       Steak and chips  No           14.00
D0004       Stuffed peppers  yes          11.50
D0005       Ham and rice     No            7.25
D0006       Lamb curry       No            8.50

饮料

Drinkid(PK)  DrinkName    DrinkType  Price
DR0001       Water        soft        1.00
DR0002       Coffee       hot         1.70
DR0003       Wine         alcoholic   3.00
DR0004       Beer         alcoholic   2.30
DR0005       tea          hot         1.50

Food_order

Food_orderno(PK)  Orderno(FK)  Dishid(FK)  NPortions
FO00001           O00001       D0003         6
FO00002           O00001       D0001         4
FO00003           O00001       D0004         3
FO00004           O00002       D0001        10
FO00005           O00002       D0002        10
FO00006           O00003       D0002        35
FO00007           O00004       D0002        23

Drink_order

Drinkorderid(PK)  Orderid(FK)  Drinkid(FK)  N_units
DO00001           O00001       DR0001       13
DO00002           O00001       DR0002       13
DO00003           O00001       DR0004       13
DO00004           O00002       DR0001       20
DO00005           O00002       DR0003       20
DO00006           O00002       DR0004       15
DO00007           O00003       DR0002       35
DO00008           O00004       DR0001       23
DO00009           O00004       DR0003       15
DO00010           O00004       DR0004       15

http://imgur.com/KziEoLo

到目前为止已经尝试过查询

select distinct d.dish_name, dr.drink_name
from Dish d, Drink dr, Food_order fo, Drink_order dro, Customer_order co
where co.orderno = fo.orderno
  AND co.orderno = dro.orderno
order by dish_name, drink_name;

select d.dish_name, dr.drink_name
from Dish d, Drink dr, Food_order fo, Drink_order dro, Customer_order co
where co.orderno = fo.orderno
  AND co.orderno = dro.orderno
group by d.dish_name, dr.drink_name;

select distinct d.dish_name, dr.drink_name
from Dish d, Drink dr, Food_order fo, Drink_order dro, Customer_order co
where d.dishid = fo.DISHID
  AND dr.DRINKID = dro.DRINKID
  AND fo.ORDERNO = dro.ORDERNO
group by d.dish_name, dr.drink_name;

select distinct d.dish_name, dr.drink_name
from dish d, drink dr, food_order fo, drink_order do
where fo.ORDERNO = do.ORDERNO
group by d.dish_name, dr.drink_name;

1 个答案:

答案 0 :(得分:0)

这似乎回归了,你要求的是什么:

SELECT
  Dish.DishName,
  Dish.Dishid,
  Drink.DrinkName,
  Drink.Drinkid
FROM Dish
JOIN
  (SELECT
      Dishid
      , Drinkid
    FROM Dish
    CROSS JOIN Drink
    MINUS
    (SELECT
      Dishid
      , Drinkid
    FROM Food_order
    CROSS JOIN Drink_order
    WHERE Food_order.Orderno = Drink_order.Orderid
    )
  ) NotOrderedTogether
  ON Dish.Dishid = NotOrderedTogether.Dishid
JOIN Drink
  ON NotOrderedTogether.Drinkid = Drink.Drinkid
ORDER BY Dishname, Drinkname
;

查看实际操作:SQL Fiddle

正如评论中所指出的,如果您添加大量(r)食物和饮料,结果集将会爆炸。在这种情况下,将MINUS首先应用于餐具和饮料可能是有意义的,并将CROSS JOIN应用于尽可能少的记录。

如果需要调整/进一步详细说明,请发表评论。