如何在Oracle中获取多对多关系查询?

时间:2013-10-07 14:35:06

标签: sql oracle

我是初学者,并尝试解决这些问题,但没有办法让任何人帮助我。

以下关系会跟踪航空公司的航班信息:

create table flights( 
     flno     number(10) primary key,
     ffrom    varchar2(50),
     fto      varchar2(50),
     distance number(10),
     departs  varchar2(50),
     arrivs   varchar2(50),
     price    number(10,2)
);

create table aircraft(
    aid            number(10)  primary key,
    aname          varchar2(50),
    cruisingrange  number(10)
);

create table employees(
    eid           number(10)  primary key,
    ename         varchar2(50),
    salary        number(10,2)
);
create table certified(
    eid           number(10) references employees(eid),
    aid           number(10) references aircraft(aid)
 );
flights

+------+-------------+----------+----------+---------+--------+-------+
| Flno |    Ffrom    |   Fto    | Distance | Departs | Arrivs | Price |
+------+-------------+----------+----------+---------+--------+-------+
| 1234 | Los Angeles | Honolulu |     1100 | 9:15    | 21:45  | 25000 |
| 1235 | Los Angeles | Honolulu |     1100 | 11:20   | 22:15  | 15000 |
| 1236 | Los Angeles | Delhi    |     3100 | 6:45    | 23:45  | 25000 |
| 1237 | London      | Newyork  |     2400 | 7:25    | 21:00  | 35000 |
+------+-------------+----------+----------+---------+--------+-------+

aircraft
+-----+-------+---------------+
| aid | aname | cruisingrange |
+-----+-------+---------------+
|   1 | Bb111 |          2000 |
|   2 | Aa123 |          5000 |
|   3 | Aa555 |           900 |
|   4 | Bb777 |           500 |
+-----+-------+---------------+

employees
+-----+-------+--------+
| Eid | Ename | Salary |
+-----+-------+--------+
| 301 | Aaa   |  14000 |
| 302 | Bbb   |  12000 |
| 303 | Ccc   |  25000 |
| 304 | Ddd   |   5000 |
| 305 | Eee   |  50000 |
| 306 | Fff   |  15000 |
| 307 | ggg   |  65000 |
+-----+-------+--------+

certified
+-----+-----+
| Eid | Aid |
+-----+-----+
| 301 |   1 |
| 302 |   3 |
| 303 |   2 |
| 306 |   4 |
| 307 |   1 |
+-----+-----+

员工关系也描述了飞行员和其他类型的员工; 每个飞行员都经过一些飞机认证,只有飞行员才有飞行认证。

我正在尝试以下查询

  1. 查找薪水低于从洛杉矶到檀香山最便宜路线价格的飞行员姓名。

  2. 对于巡航范围超过1000英里的所有飞机,找到飞机的名称和飞机认证的所有飞行员的平均工资。

  3. 我尝试了第一个查询

     select ename from employees full natural join  certified 
            where 
          salary <(select min(price) from flightsz where   ffrom='los angles' and fto='honolulu');
    

1 个答案:

答案 0 :(得分:1)

第二个是这样的:

 select aircraft.aname,avg(employees.salary) as average  from  aircraft inner join certified on 
  certified.aid =aircraft.aid  inner join employees on employees.eid = certified.eid where aircraft.cruisingrange > 1000
  group by aname

将avg功能应用于组可以获得平均工资。