oracle union 跟union all ,exists跟in
在sql 中据说使用,
exists 效率高于in,那么exists 跟in等价的sql如下,
aa跟bb表关联字段user_id
select * from aa where aa.user_id not in (select bb.user_id from bb);
select * from aa where not exists(select bb.user_id from bb where bb.user_id=aa.user_id);
select * from aa where aa.user_id in (select bb.user_id from bb);
select * from aa where exists(select bb.user_id from bb where bb.user_id=aa.user_id);
使用union 跟union all :
他们都可以关联多个表,不同的是:
union 会对查询的字段依据字段先后进行排序,去掉重复值。而union all 则不会去重复跟排序
SELECT USER_ID, USER_NAME FROM AA
UNION
SELECT USER_ID, USER_NAME FROM BB
;
SELECT USER_ID, USER_NAME FROM AA
UNION ALL
SELECT USER_ID, USER_NAME FROM BB
;