2013-10-22 59 views
1

練習39:定義「爲未來戰鬥而存活」的船隻; 在一場戰鬥中受損,他們參加了另一場戰鬥。sql-ex練習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-專家說

你的查詢產生正確的結果在主數據庫中設置的,但它 測試上失敗第二,檢查數據庫

正確的結果與我的輸出相匹配。

我在哪裏失敗?

+0

我看到的一個問題是,你沒有使用的戰鬥日期。注意「爲未來的**戰役而存活」 –

回答

2

解決了!需要添加區別

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 
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 
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 
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] 
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) 
相關問題