2016-01-09 5 views
5

La domanda è,SQL: 4 condizioni combinate con OR

determinare i nomi di tutte le navi in ​​tabella navi che soddisfano una combinazione di almeno quattro criteri dal seguente elenco: numGuns = 8 foro = 15 spostamento = 32000 type = bb lanciato = 1915 class = Kongo country = USA.

Ho trovato la risposta per questo esercizio.

La risposta è,

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 

La mia domanda è,

C'è un altro modo semplice verifica le condizioni.

+1

Si prega di dare titolo significativo. –

+1

Sì, basta spostarsi tra le clausole 'WHERE' per semplificarle. – Ruslan

+0

@Ruslan Che significa? –

risposta

4

Il cast da boolean a integer rese 0 o 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

Rigorosamente dal punto di vista predicato, sì, per esempio, gli ultimi 5 predicati:

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

può essere:

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

È possibile utilizzare CASE WHEN dichiarazioni per contare il numero di colonne di corrispondenza per ogni registra e quindi racchiude questa query per ottenere solo i record con 4 o più colonne corrispondenti.

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