SQL:4个条件与OR结合使用

时间:2016-01-09 08:00:12

标签: sql postgresql

问题是,

确定船舶表中符合以下列表中至少四个标准组合的所有船舶的名称:numGuns = 8 bore = 15 displacement = 32000 type = bb launch = 1915 class = Kongo country = USA。

我找到了这个练习的答案。

答案是,

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

我的问题是,

有没有其他简单方法检查条件。

3 个答案:

答案 0 :(得分: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 :(得分: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

答案 2 :(得分: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')))