第17章:组合查询
P114
select vend_id ,prod_id,prod_price from products where prod_price <=5 ;
select vend_id ,prod_id,prod_price from products where vend_id in (1001,1002);
P115 组合上面的两个语句。
select vend_id ,prod_id,prod_price from products where prod_price <=5 union select vend_id ,prod_id,prod_price from products where vend_id in (1001,1002); #可以看到,结果是上面两个语句的结果的和(去掉重复值),相当把两个程序使用or语句#
P116 union必须有多个select语句组成,并且每个列都包含相同的列,表达式或者聚集函数(#不懂这句话,我感觉列名一样就可以了,求解???#)
另外,union自动去重处理结果。如果不需要去重,那用union all
select vend_id ,prod_id,prod_price from products where prod_price <=5 union all select vend_id ,prod_id,prod_price from products where vend_id in (1001,1002);
P117
select vend_id ,prod_id,prod_price from products where prod_price <=5 union all select vend_id ,prod_id,prod_price from products where vend_id in (1001,1002) order by vend_id,prod_price; #使用union组合查询时,只能使用order by 自居,而且,order by 只能出现在最后一个select语句之后 #
原文地址:https://www.cnblogs.com/qiyuanjiejie/p/9439421.html