SQL检查具有两个密钥的重复记录

时间:2016-11-11 16:13:34

标签: sql oracle

是否可以将这两个语句组合成一个查询?

有两列(a_id和b_id),其他两列需要与其无关。

SELECT * FROM cases WHERE a_id=:a_id AND case_form=:case_form AND case_type=:case_type

SELECT * FROM cases WHERE b_id=:b_id AND case_form=:case_form AND case_type=:case_type

..如果有任何结果返回警告用户重复案例

2 个答案:

答案 0 :(得分:4)

两种快速方法:

SELECT * FROM cases WHERE a_id=:a_id AND case_form=:case_form AND case_type=:case_type
UNION ALL
SELECT * FROM cases WHERE b_id=:b_id AND case_form=:case_form AND case_type=:case_type

或(没有双关语!)

SELECT * FROM cases 
WHERE (a_id=:a_id AND case_form=:case_form AND case_type=:case_type) 
   OR (b_id=:b_id AND case_form=:case_form AND case_type=:case_type)

答案 1 :(得分:2)

你可以结合

SELECT * FROM cases WHERE a_id=:a_id AND case_form=:case_form AND case_type=:case_type
UNION  ALL 
SELECT * FROM cases WHERE b_id=:b_id AND case_form=:case_form AND case_type=:case_type

或者您可以使用OR

SELECT * FROM cases 
WHERE  ( a_id=:a_id  OR  b_id=:b_id ) 
AND case_form=:case_form AND case_type=:case_type