2016-01-09 18 views
5

質問は、SQL:4と組み合わせ条件OR

以下のリストからの少なくとも4つの基準の組み合わせを満たす船テーブル内のすべての船舶の名前を決定する:= 8ボアnumGuns = 15変位= 32000タイプ= bb打ち上げ= 1915クラス=コンゴ国=米国。

私はこの練習問題の答えを見つけました。

答えは、

SELECT s.name from ship s,classes c 
WHERE s.class=c.class AND 
    ((numGuns = 8 AND bore = 15 AND displacement = 32000 AND type = 'bb') 
     OR (numGuns = 8 AND bore = 15 AND displacement = 32000 AND launched = 1915) 
     OR (numGuns = 8 AND bore = 15 AND displacement = 32000 AND c.class = 'Kongo') 
     OR (numGuns = 8 AND bore = 15 AND displacement = 32000 AND country = 'USA') 
     OR (numGuns = 8 AND bore = 15 AND type = 'bb' AND launched = 1915) 
     OR (numGuns = 8 AND bore = 15 AND type = 'bb' AND c.class = 'kongo') 
     OR (numGuns = 8 AND bore = 15 AND type = 'bb' AND country = 'USA') 
     OR (numGuns = 8 AND bore = 15 AND launched = 1915 AND c.class = 'Kongo') 
     OR (numGuns = 8 AND bore = 15 AND launched = 1915 AND country = 'USA') 
     OR (numGuns = 8 AND bore = 15 AND c.class = 'Kongo' AND country = 'USA') 
     OR (numGuns = 8 AND displacement = 32000 AND type = 'bb' AND launched = 1915) 
     OR (numGuns = 8 AND displacement = 32000 AND type = 'bb' AND c.class = 'kongo') 
     OR (numGuns = 8 AND displacement = 32000 AND type = 'bb' AND country = 'USA') 
     OR (numGuns = 8 AND displacement = 32000 AND launched = 1915 AND c.class = 'Kongo') 
     OR (numGuns = 8 AND displacement = 32000 AND launched = 1915 AND country = 'USA') O 

私の質問があり、

は条件をチェックし、他の簡単な方法があります。

+1

意味のタイトルを教えてください。 –

+1

はい、それらを単純化するために 'WHERE'節のまわりを移動してください。 – Ruslan

+0

@Ruslan意味は? –

答えて

4

booleanintegerに利回り0または1からキャスト:

select s.name 
from 
    ship s 
    inner join 
    classes c using (class) 
where 
    (numguns = 8)::int + 
    (bore = 15)::int + 
    (displacement = 32000)::int + 
    (type = 'bb')::int + 
    (launched = 1915)::int + 
    (class = 'Kongo')::int + 
    (country = 'USA')::int 
    >= 4 
1

厳密述語の観点から、そう、例えば最後の5つの述語:

OR (numGuns = 8 AND displacement = 32000 AND type = 'bb' AND launched = 1915) 
OR (numGuns = 8 AND displacement = 32000 AND type = 'bb' AND c.class = 'kongo') 
OR (numGuns = 8 AND displacement = 32000 AND type = 'bb' AND country = 'USA') 
OR (numGuns = 8 AND displacement = 32000 AND launched = 1915 AND c.class = 'Kongo') 
OR (numGuns = 8 AND displacement = 32000 AND launched = 1915 AND country = 'USA') 

ができ:

OR ((numGuns = 8 AND displacement = 32000) AND 
    ((type = 'bb' AND launched = 1915) OR 
    (type = 'bb' AND c.class = 'kongo') OR 
    (type = 'bb' AND country = 'USA') OR 
    (launched = 1915 AND c.class = 'Kongo') OR 
    (launched = 1915 AND country = 'USA'))) 
2

あなたがそれぞれに一致する列の数をカウントするCASE WHENステートメントを使用することができますこのクエリをラップして、4つ以上の一致する列を持つレコードのみを取得します。

SELECT t.name 
FROM 
(
    SELECT s.name, 
     CASE WHEN s.numGuns = 8 THEN 1 ELSE 0 END AS c1, 
     CASE WHEN s.dbore = 15 THEN 1 ELSE 0 END AS c2, 
     CASE WHEN s.displacement = 32000 THEN 1 ELSE 0 END AS c3, 
     CASE WHEN s.type = 'bb' THEN 1 ELSE 0 END AS c4, 
     CASE WHEN s.launched = 1915 THEN 1 ELSE 0 END AS c5, 
     CASE WHEN c.class = 'Kongo' THEN 1 ELSE 0 END AS c6, 
     CASE WHEN s.country = 'USA' THEN 1 ELSE 0 END AS c7 
    FROM ship s INNER JOIN classes c ON s.class = c.class 
) t 
WHERE (t.c1 + t.c2 + t.c3 + t.c4 + t.c5 + t.c6 + t.c7) >= 4 
関連する問題