mysql的查询、子查询及连接查询分析(四)
#取第4、5栏目的商品,按栏目升序排列,每个栏目的商品价格降序排列,用union完成
select goods_id,goods_name,cat_id,shop_price from goods
where cat_id=4 union select goods_id,goods_name,cat_id,shop_price from goods
where cat_id=5 order by cat_id,shop_price desc;
where cat_id=4 union select goods_id,goods_name,cat_id,shop_price from goods
where cat_id=5 order by cat_id,shop_price desc;
【如果子句中有order by 需要用( ) 包起来,但是推荐在最后使用order by,
即对最终合并后的结果来排序】
即对最终合并后的结果来排序】
#取第3、4个栏目,每个栏目价格最高的前3个商品,结果按价格降序排列
(select goods_id,goods_name,cat_id,shop_price from
goods where cat_id=3 order by shop_price desc limit 3) union (select goods_id,goods_name,cat_id,shop_price from goods where cat_id=4 order by
shop_price desc limit 3) order by shop_price desc;
goods where cat_id=3 order by shop_price desc limit 3) union (select goods_id,goods_name,cat_id,shop_price from goods where cat_id=4 order by
shop_price desc limit 3) order by shop_price desc;
四、左连接,右连接,内连接 www.2cto.com
现有表a有10条数据,表b有8条数据,那么表a与表b的笛尔卡积是多少?
select * from ta,tb //输出结果为8*10=80条
1、左连接
以左表为准,去右表找数据,如果没有匹配的数据,则以null补空位,
所以输出结果数>=左表原数据数
所以输出结果数>=左表原数据数
语法:select n1,n2,n3 from ta left join tb on ta.n1= ta.n2
[这里on后面的表达式,不一定为=,也可以>,<等算术、逻辑运算符]【连接完成后,
可以当成一张新表来看待,运用where等查询】
可以当成一张新表来看待,运用where等查询】
#取出价格最高的五个商品,并显示商品的分类名称
select goods_id,goods_name,goods.cat_id,cat_name,shop_price
from goods left join category on goods.cat_id = category.cat_id order by shop_price
desc limit 5;
from goods left join category on goods.cat_id = category.cat_id order by shop_price
desc limit 5;
2、右连接
a left join b 等价于 b right join a
推荐使用左连接代替右连接
语法:select n1,n2,n3 from ta right join tb on ta.n1= ta.n2
3、内连接
查询结果是左右连接的交集,【即左右连接的结果去除null项后的并集
(去除了重复项)】
(去除了重复项)】
mysql目前还不支持 外连接(即左右连接结果的并集,不去除null项)
语法:select n1,n2,n3 from ta inner join tb on ta.n1= ta.n2
#########
例:现有表a
name hot
a 12
b 10
c 15
表b:
name hot
d 12
e 10
f 10
g 8
表a左连接表b,查询hot相同的数据
select a.*,b.* from a left join b on a.hot = b.hot
查询结果: www.2cto.com
name hot name hot
a 12 d 12
b 10 e 10
b 10 f 10
c 15 null null
从上面可以看出,查询结果表a的列都存在,表b的数据只显示符合条件的项目
再如表b左连接表a,查询hot相同的数据
select a.*,b.* from b left join a on a.hot = b.hot
查询结果为:
name hot name hot
d 12 a 12
e 10 b 10
f 10 b 10
g 8 null null
再如表a右连接表b,查询hot相同的数据
select a.*,b.* from a right join b on a.hot = b.hot
查询结果和上面的b left join a一样
###练习,查询商品的名称,所属分类,所属品牌
select goods_id,goods_name,goods.cat_id,goods.brand_id,category.cat_name,brand.brand_name
from goods left join category on goods.cat_id = category.cat_id left join
brand on goods.brand_id = brand.brand_id limit 5;
from goods left join category on goods.cat_id = category.cat_id left join
brand on goods.brand_id = brand.brand_id limit 5;
理解:每一次连接之后的结果都可以看作是一张新表
###练习,现创建如下表
create table m(
id int,
zid int,
kid int,
res varchar(10),
mtime date
) charset utf8;
insert into m values
(1,1,2,'2:0','2006-05-21'),
(2,3,2,'2:1','2006-06-21'),
(3,1,3,'2:2','2006-06-11'),
(4,2,1,'2:4','2006-07-01');
create table t www.2cto.com
(tid int,tname varchar(10)) charset utf8;
insert into t values
(1,'申花'),
(2,