# 七、union联合

Union：联合

1.Select * from goods where shop_price < 20 or shop_price >5000;
2.我们用union将他们合并就是
Select * from goods where shop_price < 20
Select * from goods where shop_price > 5000

Select * from goods where shop_price < 20 union Select * from goods where shop_price > 5000

Union的语法要求：

1张为
Ta

Id num
a,  5
b,  10
c,  15
d,  10

Tb

Id   num
b    5
c    10
d    20
e    99

a,5
b,15
c,25
d,30
e,99

select id,sum(num) from (select * from ta union select * from tb) as tmp group by id;

Select * from ta union all select * from tb;

Ta

Id num
a,  5
b,  10
c,  15
d,  10

Tb

Id   num
b    5
c    15
d    20
e    99

select id,sum(num) from (select * from ta union all select * from tb) as tmp group by id;

(select goods_id,cat_id,goods_name,shop_price from goods where cat_id = 4) union (select goods_id,cat_id,goods-name,shop_price from goods where cat_id = 5) order by shop_price;

select goods_id,cat_id,goods_name,shop_price from goods where cat_id = 3 order by shop_price desc limit 3unionselect goods_id,cat_id,goods_name,shop_price from goods where cat_id = 4 order by shop_price desc limit 2;