2016-01-09 7 views
5

pytanie,SQL: 4 Warunki połączone lub

Ustalić nazwy wszystkich statkach na statkach tabeli, odpowiadających kombinację co najmniej czterech kryteriów, z poniższej listy: numGuns = 8 otworu = 15 przemieszczenia = 32000 type = bb launch = 1915 class = Kraj Kongo = USA.

Znalazłem odpowiedź na to ćwiczenie.

Odpowiedź brzmi

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 

Moje pytanie brzmi,

Czy istnieje inny prosty sposób sprawdzić warunki.

+1

proszę dać sensowne tytuł. –

+1

Tak, po prostu poruszaj się wokół klauzul "WHERE", aby je uprościć. – Ruslan

+0

@Ruslan Co oznacza? –

Odpowiedz

4

Obsada z boolean do integer plonów 0 lub 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

Ściśle z bazowego punktu widzenia, tak, dla przykładu ostatnie 5 predykaty:

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') 

może być:

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

Można używać CASE WHEN oświadczenia policzyć liczbę pasujących kolumn dla każdego rekord, a następnie zawiń to zapytanie, aby uzyskać tylko rekordy z 4 lub więcej zgodnymi kolumnami.

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