SQL查询语法检查

时间:2016-12-02 13:49:06

标签: sql oracle

有人可以为我写这个要求。我似乎无法在所有其他条件下加载空值。

- (1)没有艺术家是80岁或以上 - 当他们没有50或更年轻时 - 死了。

- (2)没有艺术家年龄54,56岁, - 他们去世时的71或76岁。

- (3)有些艺术家还活着。

- (4)德国和美国的艺术家 - 专注于摄影艺术(' Photo') - 被排除在外。

- (5)没有艺术家有字母' o' - 姓氏的任何地方。

- (6)没有艺术家有名字 - ' Hannah' Julia'或者' Frasier'。

select artist_id, first_name, last_name, died - born AS age, speciality, nationality
from simon_antiques
where Died - born Not in (54, 56, 71, 76)
  AND first_name NOT IN ('Hannah', 'Julia', 'Frasier')
  AND last_name not like '%o%'
  AND (nationality <> 'German' or speciality <> 'Photo')
  AND (died - born between 50 and 80 or died is null)
Order by last_name

这会加载所有结果,但不会加载仍然活着的艺术家。如果我将条件从括号中移出,它将带回所有活着的人,无论其他条件如何。

1 个答案:

答案 0 :(得分:2)

WHERE中的第一个子句仅匹配died不为空的行。鉴于此,您可以对子句重新排序:

WHERE first_name NOT IN ('Hannah', 'Julia', 'Frasier')
  AND last_name NOT LIKE '%o%'
  AND (nationality <> 'German' 
    OR speciality <> 'Photo')
  AND ((died - born BETWEEN 50 AND 80 
      AND Died - born NOT IN (54, 56, 71, 76)) 
    OR died IS NULL)

这应该带回那些当前活着的人。