仅在第一个WHERE条件不满足时才执行第二个WHERE条件

时间:2018-05-25 20:31:22

标签: mysql hive

我有一组WHERE条件,我的要求是条件2应该只在不满足条件1后执行,下面是我的查询

SELECT (B.col1, B.col2, B.col3)
FROM
(SELECT firstname, lastname FROM employee) A
INNER JOIN
(SELECT col1, col2, col3 FROM office) B
ON 
TRIM(A.firstname) = TRIM(B.col1) AND 
INNER JOIN 
(SELECT col4, col5, col6 FROM school) C
ON
TRIM(A.firstname) = TRIM(C.col4) 
WHERE (condition1) OR (condition2) OR (condition3) OR (condition4) OR (condition5) OR (condition6);

1 个答案:

答案 0 :(得分:-1)

您需要单独的查询。它可以在单个查询中完成,但会非常复杂并且可能非常低效。给出这样一个(可怕的)查询的简洁例子以供参考:

SELECT [stuff]
FROM [stuff2: tables, joins, etc....]
WHERE [condition1]
   OR ([condition2] AND NOT EXISTS (SELECT * FROM [stuff2... yes, copy everything from the first stuff2] WHERE [condition1]))
   OR ([condition3] AND NOT EXISTS (SELECT * FROM [stuff2] WHERE [condition1] OR [condition2]))
   OR ([condition4] AND NOT EXISTS (SELECT * FROM [stuff2] WHERE [condition1] OR [condition2] OR [condition3]))
   and so on...

因此,只需分别查询每个此类条件,直到获得结果或用完条件进行检查。