UNION 和 UNION ALL的区别,一个例子就看明白

【UNION ALL】

select a,b,sum(sm) AS s1, SUM(qm) AS s2 from   
(  
 select 'a' AS a, 'b' AS b, 2 AS sm, 200 AS qm   
UNION ALL  
 select 'a' AS a, 'b' AS b, 2 AS sm, 200 AS qm  
)r  
group by a,b  
ab4400

【UNION】

select a,b,sum(sm) AS s1, SUM(qm) AS s2 from   
(  
 select 'a' AS a, 'b' AS b, 2 AS sm, 200 AS qm   
UNION   
 select 'a' AS a, 'b' AS b, 2 AS sm, 200 AS qm  
)r  
group by a,b 
ab2200

总结:UNION ALL全部列出,UNION会去掉重复的。

你可能感兴趣的:(sql)