现有4个表
classes 船只类型表 主要字段有 class country 排水量
ships 表 主要字段有 name class 下水年份
battles 表 主要字段有 btname date
outcomes 表 主要字段有 ship btname result (值为 "沉没","损坏","OK") 现要做2个查询: 1.查询哪些船只在某场战争中 损坏 但在后面又投入战争
2.查询哪些战争 有国家出动3艘或以上的船只
望各位高手赐教!
classes 船只类型表 主要字段有 class country 排水量
ships 表 主要字段有 name class 下水年份
battles 表 主要字段有 btname date
outcomes 表 主要字段有 ship btname result (值为 "沉没","损坏","OK") 现要做2个查询: 1.查询哪些船只在某场战争中 损坏 但在后面又投入战争
2.查询哪些战争 有国家出动3艘或以上的船只
望各位高手赐教!
select *
from outcomes
where result ='损坏' and btname in (select btname from battles )
from outcomes
where result ='损坏' and btname in (select btname from battles )恕我愚昧,这个查询如何确保 这个损坏的船只日后又参战了?
select btname,class ,count(ship)
from outcomes left join ships on ship= name
group by btname,class
select * from outcomes a where a.result='损坏' and exists (select 1 from battles where a.btname=btname)
ships 表 主要字段有 shipname(船名) class (类型) year (下水年份) country (所属国家)
battles 表 主要字段有 btname(战争名称) date(战争日期)
outcomes 表 主要字段有 ship(船名) btname(战争名称) result (值为 "沉没","损坏","OK")
select distinct o.ship from outcome o,
(select a.*,date from outcome a,battles b where a.btname=b.btname and result='bad') m,
(select a.*,date from outcome a,battles b where a.btname=b.btname and result='ok') n
where m.ship=n.ship and m.date<n.date
and m.ship=o.ship
看下这个行么??
select ship from outcomes o where result='损坏' and exists(select 1 from battles b left join outcomes ou on b.btname=ou.btname where b.date>(select ba.date from battles ba where ba.btname=o.btname) and ou.ship=o.ship)
classes 表 主要字段有 class(船只类型) pailiang( 排水量)
ships 表 主要字段有 shipname(船名) class (类型) year (下水年份) country (所属国家)
battles 表 主要字段有 btname(战争名称) date(战争日期)
outcomes 表 主要字段有 ship(船名) btname(战争名称) result (值为 "沉没","损坏","OK")
现要做2个查询:
*/
--1.查询哪些船只在某场战争中 损坏 但在后面又投入战争
select btname from battles where btname in(select btname from outcomes where result='损坏')--2.查询哪些战争 有国家出动3艘或以上的船只
select count(*) as cc,OO.ship,OO.btname from outcomes OO join battles BB
OO.btname=BB.btname where cc>3
exists (select 1 from outcomes o2,battles bt2 where o2.btname=bt.btname and bt2.date>bt1.date and o2.ship=o1.ship)
2.
select btname,country ,count(*) from battles bt,outcomes ou,classes cl,ships sh
where cl.class=sh.class and sh.name=ou.ship and bt.btname==ou.btname group by btname,country having count(*)>=3
select distinct btname(select btname,country,count(*) from battles bt,outcomes ou,classes cl,ships sh
where cl.class=sh.class and sh.name=ou.ship and bt.btname==ou.btname group by btname,country having count(*)>=3)sss
1:select * from (select * from battles where btname="某场战争" inner join outcomes
on battles.btname=outcomes.btname)where result="损坏"......好难,进行不下去了