码迷,mamicode.com
首页 > 数据库 > 详细

理解 sql 的子查询

时间:2015-09-12 00:44:54      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:

子查询

子查询

Table of Contents

来源: 网上的公开课

数据准备

create table goods (
        goods_id mediumint(8) unsigned primary key auto_increment,
        goods_name varchar(120) not null default ‘‘,
        cat_id smallint(5) unsigned not null default ‘0‘,
        brand_id smallint(5) unsigned not null default ‘0‘,
        goods_sn char(15) not null default ‘‘,
        goods_number smallint(5) unsigned not null default ‘0‘,
        shop_price decimal(10,2) unsigned not null default ‘0.00‘,
        market_price decimal(10,2) unsigned not null default ‘0.00‘,
        click_count int(10) unsigned not null default ‘0‘
        ) engine=myisam default charset=utf8;

insert into `goods` values (1,‘kd876‘,4,8,‘ecs000000‘,1,1388.00,1665.60,9),
    (4,‘诺基亚 n85 原装充电器‘,8,1,‘ecs000004‘,17,58.00,69.60,0),
    (3,‘诺基亚原装 5800 耳机‘,8,1,‘ecs000002‘,24,68.00,81.60,3),
    (5,‘索爱原装 m2 卡读卡器‘,11,7,‘ecs000005‘,8,20.00,24.00,3),
    (6,‘胜创 kingmax 内存卡‘,11,0,‘ecs000006‘,15,42.00,50.40,0),
    (7,‘诺基亚 n85 原装立体声耳机 hs-82‘,8,1,‘ecs000007‘,20,100.00,120.00,0),
    (8,‘飞利浦 9@9v‘,3,4,‘ecs000008‘,1,399.00,478.79,10),
    (9,‘诺基亚 e66‘,3,1,‘ecs000009‘,4,2298.00,2757.60,20),
    (10,‘索爱 c702c‘,3,7,‘ecs000010‘,7,1328.00,1593.60,11),
    (11,‘索爱 c702c‘,3,7,‘ecs000011‘,1,1300.00,0.00,0),
    (12,‘摩托罗拉 a810‘,3,2,‘ecs000012‘,8,983.00,1179.60,13),
    (13,‘诺基亚 5320 xpressmusic‘,3,1,‘ecs000013‘,8,1311.00,1573.20,13),
    (14,‘诺基亚 5800xm‘,4,1,‘ecs000014‘,1,2625.00,3150.00,6),
    (15,‘摩托罗拉 a810‘,3,2,‘ecs000015‘,3,788.00,945.60,8),
    (16,‘恒基伟业 g101‘,2,11,‘ecs000016‘,0,823.33,988.00,3),
    (17,‘夏新 n7‘,3,5,‘ecs000017‘,1,2300.00,2760.00,2),
    (18,‘夏新 t5‘,4,5,‘ecs000018‘,1,2878.00,3453.60,0),
    (19,‘三星 sgh-f258‘,3,6,‘ecs000019‘,12,858.00,1029.60,7),
    (20,‘三星 bc01‘,3,6,‘ecs000020‘,12,280.00,336.00,14),
    (21,‘金立 a30‘,3,10,‘ecs000021‘,40,2000.00,2400.00,4),
    (22,‘多普达 touch hd‘,3,3,‘ecs000022‘,1,5999.00,7198.80,16),
    (23,‘诺基亚 n96‘,5,1,‘ecs000023‘,8,3700.00,4440.00,17),
    (24,‘p806‘,3,9,‘ecs000024‘,100,2000.00,2400.00,35),
    (25,‘小灵通/固话 50 元充值卡‘,13,0,‘ecs000025‘,2,48.00,57.59,0),
    (26,‘小灵通/固话 20 元充值卡‘,13,0,‘ecs000026‘,2,19.00,22.80,0),
    (27,‘联通 100 元充值卡‘,15,0,‘ecs000027‘,2,95.00,100.00,0),
    (28,‘联通 50 元充值卡‘,15,0,‘ecs000028‘,0,45.00,50.00,0),
    (29,‘移动 100 元充值卡‘,14,0,‘ecs000029‘,0,90.00,0.00,0),
    (30,‘移动 20 元充值卡‘,14,0,‘ecs000030‘,9,18.00,21.00,1),
    (31,‘摩托罗拉 e8 ‘,3,2,‘ecs000031‘,1,1337.00,1604.39,5),
    (32,‘诺基亚 n85‘,3,1,‘ecs000032‘,4,3010.00,3612.00,9);


create table category (
        cat_id smallint unsigned auto_increment primary key,
        cat_name varchar(90) not null default ‘‘,
        parent_id smallint unsigned
        )engine myisam charset utf8;


INSERT INTO `category` VALUES
    (1,‘手机类型‘,0),
    (2,‘CDMA 手机‘,1),
    (3,‘GSM 手机‘,1),
    (4,‘3G 手机‘,1),
    (5,‘双模手机‘,1),
    (6,‘手机配件‘,0),
    (7,‘充电器‘,6),
    (8,‘耳机‘,6),
    (9,‘电池‘,6),
    (11,‘读卡器和内存卡‘,6),
    (12,‘充值卡‘,0),
    (13,‘小灵通/固话充值卡‘,12),
    (14,‘移动手机充值卡‘,12),
    (15,‘联通手机充值卡‘,12);

查询出最新一行商品(以商品编号最大为最新, 用子查询实现)

select * from goods where goods_id = (select max(goods_id) from goods)\G
select * from goods order by goods_id desc limit 1\G

查询出编号为 19 的商品的栏目名称(用左连接查询和子查询分别)

select category.* from category left join goods using (cat_id) where goods.goods_id = 19;
select category.* from category where cat_id = (select cat_id from goods where goods_id = 19);

用 where 型子查询把 goods 表中的每个栏目下面最新的商品取出来

select goods.*  from goods where goods_id in (select max(goods_id) from goods group by cat_id)\G

用 from 型子查询把 goods 表中的每个栏目下面最新的商品取出来

select * from (select goods_id, cat_id, goods_name from goods order by goods_id desc) as tmp group by cat_id;  -- 课上给的正确答案

用 exists 型子查询, 查出所有有商品的栏目

select * from category where exists (select * from goods where goods.cat_id=category.cat_id);
select * from category where category.id in (select goods.cat_id from goods);

总结:

子查询有 where, on, from 3 种, 分别对应 查询结果是 一行一列, 多行一列, 和多行多列
from 型的子查询需要对查询后的数据进行重命名到另一个表

理解 sql 的子查询

标签:

原文地址:http://www.cnblogs.com/sunznx/p/4802369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!