1

练习 39:定义“为未来的战斗幸存”的舰船;在一场战斗中受伤,他们参加了另一场战斗。

数据库架构:http ://www.sql-ex.ru/help/select13.php#db_3

我的做法:

SELECT distinct o.ship from Outcomes o
WHERE o.RESULT = 'damaged' 
AND exists (select 1 FROM Outcomes o2 WHERE o2.ship = o.ship 
AND (o2.result='OK' OR o2.result='sunk'))

sql-ex 说

您的查询在主数据库上产生了正确的结果集,但第二次测试失败,检查数据库

正确的结果与我的输出匹配。

我哪里失败了?

4

6 回答 6

2

解决了!需要添加 Distinct

select distinct kk.ship from
(Select ship,date from outcomes oo,battles bb where oo.battle=bb.name) tt 
inner join 
(Select ship,date as damagedate from outcomes oo,battles bb where result='damaged' AND oo.battle=bb.name) kk 
ON tt.ship=kk.ship AND tt.date>kk.damagedate
于 2016-03-29T07:19:56.443 回答
1
with a as (select * from outcomes o join battles b on o.battle=b.name)select distinct a1.ship from a a1, a a2 where a1.ship=a2.ship and a1.result='damaged'and a1.date<a2.date
于 2013-12-13T06:46:52.043 回答
0

试试这个:

    select o.ships from (select a.ships, a.battle from outcomes as a where result in (damaged, unharmed)) as o
    where o.battle <> a.battle
于 2013-10-22T18:54:39.657 回答
0
select distinct outcomes.ship
from outcomes, (select outcomes.ship, battles.[date], outcomes.result
            from outcomes, battles
            where battles.name = outcomes.battle
            and result = 'damaged' ) t1, 
 battles
where outcomes.ship = t1.ship and
outcomes.battle = battles.name and
battles.[date] > t1.[date]
于 2015-06-09T12:56:09.453 回答
0

这是我的答案,该页面并未将其解析为真实,但我不同意。不知道我的解决方案与您的解决方案有何不同

select distinct damaged.ship from 
(Select oo.ship,date from outcomes oo
inner join battles bb on oo.battle=bb.name
) as ships
inner join (Select ship,date as damagedate from outcomes oo1
inner join battles bb1 on oo1.battle=bb1.name and result = 'damaged') as damaged
on (ships.ship = damaged.ship and ships.date != damaged.damagedate)
于 2016-10-06T11:44:03.130 回答
0

也许这不是最伟大的设计,但也为我工作:

with damaged as (Select b.date, o.ship
from outcomes o 
inner join battles b on b.name = o.battle
where result = 'damaged') 

Select distinct o.ship
from outcomes o 
right join damaged d on d.ship = o.ship
right join battles b on b.name = o.battle
where b.date > d.date
于 2020-10-12T17:25:15.790 回答