您的位置:首页 > 数据库 > MySQL

MySQL中的Union连接查询

2012-06-20 21:37 429 查看
select
union的用法
作用:把两次或多次的查询结果合并起来
要求:两次查询的列数一致就好
推荐:查询的每一列,相对用的列类型也一样。
可以来自于多张表。
多次SQL语句取出的列明可以不一致,此时以第一个SQL的列名为准。
如果不同的语句中取出的行,有完全相同(每个列的值都相同)
那么相同的行将会合并(去重复);
如果不去重复,可以加all来指定。
请注意:
如果子句中有order by,limit,须加(),推荐放到所有子句之后,即对最终合并后的结果排序。
在子句中,order by配合limit使用才有意义。
如果order by和limit不配合使用,会被语法分析器优化分析时去除。
***********************合并查询结果*******************
#查询网店商品价格小于50的商品信息
selelct good_id,goods_name,shop_price from goods where shop_price<50;
#把价格大于5000或者小于20的商品查询出来
select goods_id,goods_name,shop_price from goods where shop_price<20 or shop_price>5000;
select goods_id,goods_name,shop_price from goods where shop_price > 5000 union select goods_id,goods_name,shop_price from goods where shop_price < 20;
#把允许显示的留言显示出来
select user_name,user_email,msg_content from feedback where msg_status=1;
#把允许显示的评论取出来
select user_name,email,content from content where status=1;
#利用union简化为一条查询
*************************连接查询*********************
数学上:
集合 set
集合的特性:无序性,唯一性。
一张表就是一个集合,一行数据是集合的一个元素。
理论上讲,不可能存在完全相同的两个行,但是表中可以完全相同的两行,因为,表内部有一个rowid
集合相乘就是笛卡尔积,其实就是两个集合的完全组合
问:设集合A有M个元素,M个元素各不相同。设集合B,有N个元素,N个元素各不相同。
A*B,得到的积,有M*N个元素,不可能重复。
表A有9行,表B有10行,两表相乘,有9*10=90行数据。
ta表
id num
a 5
b 10
c 15
d 10
tb表
id num
b 5
c 10
d 20
e 99

create table ta(
id char(1),
num int
);

insert into ta values
('a',5),
('b',10),
('c',15),
('d',10);

create table tb(
id char(1),
num int
);

insert into ta values
('b',5),
('c',10),
('d',20),
('e',90);

想要得到的结果
a 5
b 15
c 25
d 30
e 90

select * from ta;
select * from tb;
****************************************
select * from ta union select * from tb;
#sum,group求和
select id,sum(num) from (select * from ta union select * from tb) as temp group by id;
update ta set num=15 where id='c';
select * from ta union all select * from tb;
#想取第4栏目的商品,价格降序排列,还想取第5个栏目的商品,价格也降序排列。union完成。
(select goods_id,cat_id,goods_name,shop_price from goods where cat_id=4 order by shop_price desc) union (select goods_id,goods_name,shop_price from goods where cat_id =5 order by shop_price desc) order by desc;
#取第3个栏目价格前3高的商品和第4个栏目价格前两高的商品,用union实现。
(select goods_id,cat_id,goods_name,shop_price from goods where cat_id=4 order by shop_price desc limit 3)union(select goods_id,cat_id,goods_name,shop_price from goods where cat_id=4 order by shop_price desc limit 2);

select * from ta,tb;相当于,ta*tb之后再查。完全组合。
*************************左连接********************
select 列1,列2,列3...列N from
tableA left join tableB
on tableA.列=tableB[此时,表连接成一张大表,完全当成普通表看]
where group,having...照常写
要从goods,category表中取数据

然后,在两张M*N的过程中,筛选,以什么条件筛选,答:cate_id相同。
1、先接上表;
2、是连接条件。
select goods left join category on goods.cat_id=category.cat_id;
#两张表连接成了一张大表。
select
goods_id,good.cat_id,cat_name,goods_name,shop_price
from
goods left join category on goods.cat_id=category.cat_id;
*****************************右连接******************
elect 列1,列2,列3...列N from
tableA right join tableB
on tableA.列=tableB[此时,表连接成一张大表,完全当成普通表看]
where group,having...照常写
*****************************内连接******************
elect 列1,列2,列3...列N from
tableA inner join tableB
on tableA.列=tableB[此时,表连接成一张大表,完全当成普通表看]
where group,having...照常写
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: