• 一键推广
淘教中国网
家教板块
北京市
全国免费咨询
400-0000-696

首页 > 淘教百科 > MySQL中的Union连接查询

MySQL中的Union连接查询

  时间:2013-06-13 14:03:28 [打印] 浏览1713次

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个元素各不相同。
AB,得到的积,有MN个元素,不可能重复。
表A有9行,表B有10行,两表相乘,有910=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相当于,tatb之后再查。完全组合。
左连接
select 列1,列2,列3...列N from
tableA left join tableB
on tableA.列=tableB[此时,表连接成一张大表,完全当成普通表看]
where group,having...照常写
要从goods,category表中取数据

然后,在两张MN的过程中,筛选,以什么条件筛选,答: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...照常写

点击报名
注:转载请注明:文章摘自淘教中国网http://www.taoedu.cn。如有异议,请联系我们。
上一篇: 2012年9月计算机等级考试三级网络技术上机南开一百题
下一篇: 计算机的域

淘教百科

网站公告

Powered by taoedu.cn1.3.0 © 导航_淘教中国(www.taoedu.cn) 京ICP备11038813号-1 | 京ICP证120702
服务热线:4000000696(呼叫中心技术支持) | 邮编:100088
公司名称:哈哈莉莉(北京)网络科技有限公司    独家运营    公司资质
地址:北京市西城区德胜门外大街11号B座511室(中关村科技园区德胜科技园内)

网络警察 国家备案 北京工商 支付宝 360

京公网安备 11010202007088号

   

学业规划咨询