2

mysql データベースのスクリプトは次のとおりです。

Create Table Aircraft
(
    aid integer not null,
    aname nchar(30) not null,
    cruisingrange integer not null,
    primary key (aid)   
);


Create Table Flights
(
    flNo integer not null,
    flFrom nchar(20)not null,
    flTo nchar(20) not null,
    distance integer not null,
    departs time not null,
    arrives time not null,
    price Decimal(6,2) not null,
    primary key(flNo)
);


create table Pilots
(
    pid integer not null,
    pname nchar(20) not null,
    salary Decimal(6,2) not null,
    primary key(pid)
);


create table certified
(
    pid integer not null,
    aid integer not null,
    primary key(pid,aid),
    foreign key(pid) references Pilots(pid)  ON DELETE CASCADE,
    foreign key(aid) references Aircraft(aid) ON DELETE CASCADE
);


INSERT INTO Aircraft(aid,aname,cruisingrange)
VALUES
(1, 'B-450',10000),
(2, 'C-190',4000),
(3, 'RN-110',5000),
(4, 'kp-30',2000),
(5, 'sh-60',1500),
(6, 'mr-70',7000),
(7, 'VK-20',3500);


INSERT INTO Flights(flNo,flFrom,flTo,distance,departs,arrives,price)
VALUES
(100,'city1','city2',1200,'16:00:00','16:30:00',130),
(110,'city3','city4',1000,'18:00:00','19:00:00',160),
(112,'city5','city6',2000,'15:00:00','16:00:00',185),
(115,'city7','city8',4000,'14:00:00','16:00:00',250),
(118,'city9','city3',1500,'18:00:00','19:00:00',220),
(119,'city2','city3',2500,'20:00:00','21:30:00',180);


INSERT INTO Pilots(pid,pname,salary)
VALUES
(400,'jack',150),
(410,'pit',180),
(420,'nami',200),
(430,'rafel',110),
(440,'linda',300);


INSERT INTO Certified(pid,aid)
VALUES 
(400,1),
(400,6),
(410,1),
(420,1),
(420,3),
(420,7),
(440,4),
(440,6);

認定されたすべてのパイロットの給与が 187 ドルを超える航空機の名前を検索するクエリが必要です。実は、その「すべて」が私の問題です!誰でも私を助けてもらえますか?

4

2 に答える 2

4

この問題は、認定パイロットの最低給与が 187 ドル以上の飛行機を見つけることと言い換えることができます。

これを SQL に変換するのは簡単です。

SELECT aid
FROM ... all your joins here ...
GROUP BY aid
HAVING MIN(salary) >= 187
于 2012-11-09T18:28:30.850 に答える
1

私はあなたが探している187ドルを持っているパイロットだけの事前クエリから始めて、それから彼らが認定されている飛行機を見つけます...

select
      QualifiedPilots.*,
      A.AName,
      A.CruisingRange
   from
      ( select 
              P.*
           from
              Pilots P
           where
              P.Salary >= 187 ) as QualifiedPilots
         JOIN Certified C
            on QualifiedPilots.pid = C.pid
            JOIN Aircraft A
               on C.aid = A.aid

あなたがOUTを望んでいたことについての質問とあなたのコメントが曖昧であるため、パイロットごとにgroup_concatを含めるように改訂しました。

select
      QualifiedPilots.*,
      group_concat( A.AName ) CertAircraft
   from
      ( select 
              P.*
           from
              Pilots P
           where
              P.Salary >= 187 ) as QualifiedPilots
         JOIN Certified C
            on QualifiedPilots.pid = C.pid
            JOIN Aircraft A
               on C.aid = A.aid
   group by
      QualifiedPilots.pid

この結果は、2人のパイロットのみを示しています...3機の航空機で認定されたパイロット「nami」と2機の航空機で認定された「linda」を示しています...最初のクエリが返すものである合計5つの認定認定...同じ名前です複数回表示されますが、航空機の詳細が表示されます。

于 2012-11-09T18:40:35.183 に答える