我有一个问题我被困住了,我无法弄清楚如何获得正确的查询。首先,这个问题是参考www.sql-ex.ru及其在DML部分的练习10。
以下是所有信息:
正在考虑参加第二次世界大战的海军舰艇数据库。该数据库具有以下关系:
Classes(class, type, country, numGuns, bore, displacement)
Ships(name, class, launched)
Battles(name, date)
Outcomes(ship, battle, result)
课程中的船舶安排在一个项目中。通常会为一类人员分配正在考虑的类别中的第一艘船的名称(船头);否则,类名与数据库中的任何船名不一致。 Classes关系包括班级名称,类型(战斗船的bb,战斗巡洋舰的bc),建造船舶的国家,主炮的数量,枪口径(枪管的直径,以英寸为单位),以及排量(重量以吨计)。船舶关系包括船名,船级名称和发射年份。战斗关系包括船舶参与战斗的名称和日期;而他们参与战斗的结果(沉没,受损或无伤害 - 好的)是在结果关系中。 注:1)结果关系可能包括未包含在船舶关系中的船舶。 2)沉船在此之后不能参加战斗。
提示任务#14
delete from classes
where class not in (
select distinct c.class from classes c, ships s
where c.class = s.class
group by c.class
having count(name) >= 3
union
select distinct c.class from classes c, outcomes o
where c.class = o.ship
group by c.class
having count(ship) >= 3
union
select distinct s.class from ships s, outcomes o
where s.class = o.ship
group by s.class
having count(name) >= 3)
答案 0 :(得分:0)
delete from classes
where class not in (
select t.klas
from
(select class klas, count(class)no
from ships
group by class
union all
select ship klas, count(ship)no
from outcomes
where ship not in (select s1.name from ships s1)
group by ship) t
group by t.klas
having sum(t.no) >= 3)
答案 1 :(得分:0)
DELETE FROM classes
WHERE class IN (SELECT class
FROM ships
GROUP BY class
HAVING Count(NAME) < 3);