Mysql基础+select5种子句 + 子查询

2,750 阅读7分钟

1、创建练习的表

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);


查询中用到的关键词主要包含六个,并且他们的顺序依次为
select--from--where--group by--having--order by

2、Where 条件查询

①where expression

用法:expression为真,则该行取出

运用场合

各种条件查询场合,如按学号查学生,按价格查商品,按发布时间查新闻等

②select 5种子句 之where常用运算符

③select 5种子句 之where 匹配

like 模糊匹配 % 通配任意字符 _ 通配单一字符

2.1 取出goods表价格低于或等于100元的商品

select goods_id,cat_id,goods_name,shop_price from goods where shop_price <= 100;


2.2 取出第4栏目和第11栏目的商品(不许用or)

select goods_id,cat_id,goods_name,shop_price  from goods where cat_id in (4, 11);


2.3 取出100<=价格<=500的商品(不许用and)

select goods_id,cat_id,goods_name,shop_price  from goods where shop_price between 100 and 500;


2.3 取出名字以"诺基亚"开头的商品

select goods_id,cat_id,goods_name,shop_price  from goods where goods_name like '诺基亚%';


2.4 取出名字为"诺基亚Nxx"的手机

select goods_id,cat_id,goods_name,shop_price  from goods where goods_name like '诺基亚n__';

2.5 查出本店价格比市场价格省200元以上的商品

select goods_id, goods_name,abs(market_price-shop_price) as discount from goods
where (market_price-shop_price)>200;

这里查询要出错,因为 market_price和shop_price的字段是decimal(10,2)unsigned

unsigned字段相减,不能为负数,我不知道为什么,mysql就是不行。。。

解决方案,把decimal(10,2) unsigned 改为decimal(10,2)

alter table goods modify column shop_price  decimal(10,2);
alter table goods modify column market_price  decimal(10,2);

3、group by 分组

①select 5种子句 之group与统计函数

max : 求最大 min : 求最小 sum : 求总和 avg : 求平均 count:求总行数

②select 5种子句 之group介绍

group by

作用:把行 按 字段 分组

语法:group by col1,col2,...colN

运用场合

常见于统计场合,如按栏目计算帖子数, 统计每个人的平均成绩等.

3.1 group与统计函数

  • max: 求最大值
  • min: 求最小值
  • sum: 求总和
  • avg: 求平均
  • count: 求总行数(count不能识别null)


3.2 计算每一种商品的库存数量之和

select goods_name,sum(goods_number) from goods group by goods_name;


4、having 筛选

①select 5种子句 之having介绍

having 与where异同点

having 与where类似,可筛选数据
where后的表达式怎么写,having就怎么写

where针对表中的列发挥作用,查询数据

having针对查询结果中的列发挥作用,筛选数据

4.1 查询每个栏目下,积压的货款,且筛选出积压金额>20000的栏目

select cat_id,sum(goods_number*shop_price) as sumMoney from goods group by cat_id 
having sumMoney > 20000


4.2 where-having-group综合练习题

# 建表
create table result (
    name varchar(20),
    subject varchar(20),
    score tinyint
)engine myisam charset utf8;


# 插入数据
insert into result
values
('张三','数学',90),
('张三','语文',50),
('张三','地理',40),
('李四','语文',55),
('李四','政治',45),
('王五','政治',30)


# 查询出2门及2门以上不及格者的平均成绩
select name,sum(score < 60) as gk ,avg(score) as pj from result group by name having gk >=2;

5、order by 排序

①select 5种子句 之order排序

Order by 排序功能

按一个或多个字段对查询结果进行排序

用法:order by col1,col2,col3

知识点的运用场合描述

各种排序场合,如新闻按点击量排序, 商品按价格排序等

默认排序:升续排列

5.1 按栏目升序排列,同一栏目下的商品,再按商品的价格降序排列

# asc代表升序,desc代表降序
select goods_id,cat_id,goods_name,shop_price
from goods
order by cat_id asc, shop_price desc

6、limit 限制结果条数

①select 5种子句 之limit 介绍

Limit 限制条数

limit [offset,] N,限制结果取N条

用法: limit [偏移量,],取出条目

知识点的运用场合描述

分页应用中最为典型,如第1页取1-20条,第2页取21-40条.

6.1 取出栏目3下,价格降序排列的前10条数据

select goods_id, cat_id, goods_name, shop_price
from goods
where cat_id = 3
order by shop_price asc
limit 10;

6.2 查询本店商品价格从高到底排序的第三名到第五名的商品

select goods_id, goods_name, shop_price
from goods
order by shop_price
desc
limit 2, 3;
# limit 2,3中,2代表偏移量,从3个开始数,3代表要3条数据。。。。。。。。。。。。。。。。。。。。。


7、where子查询

7.1 查询每个栏目下goods_id最大的商品

1、首先查出每个栏目下的goods_id
select max(goods_id),cat_id from goods
group by cat_id
2、把上面的查询结果作为where的子句
select goods_id, goods_name from goods
where goods_id in
(select max(goods_id) from goods group by cat_id);


8、from子查询

8.1 查询每个栏目下goods_id最大的商品

1、首先查出每个栏目下商品价格从高到底排序的结果
select goods_id, goods_name from goodsorder by cat_id, shop_ price desc
2、把上面的查询结果作为from的子句
select * from 
(select goods_id,cat_id, goods_name from goods order by cat_id asc, goods_id desc)
 as tmp group by cat_id;


9、exists子查询

# 需要再建一张表,结合上面的goods表练习
create table category(
    cat_id int auto_increment primary key,
    cat_name varchar(20) not null default ''
)engine myisam charset utf8;

# 插入数据
insert into category
(cat_name)
values 
('手机类型'),
('CDMA手机'),
('GSM手机'),
('3G手机'),
('双模手机'),
('手机配件'),
('充电器'),
('耳机'),
('电池'),
('读卡器和内存'),
('充值卡'),
('小灵通/固话充值卡'),
('移动手机充值卡'),
('联通手机充值卡');


9.1 把栏目下有商品的商品栏目取出来(不是每个cat_id里都有商品)

select cat_id,cat_name from category where exists 
(select * from goods where goods.cat_id = category.cat_id);

10、为什么建表的时候要写not null default ''

# 我们先建立一张表
create table test9(
    sname varchar(20)
)engine myisam charset utf8;

# 插入数据
insert into test9
values
('lisi'),
('wangwu'),
(null);


请对比以下两张图

select sname from test9 where sname != null;

select sname from test9 where sname = null;

这就奇怪了,!=null得到空集,=null也是空集。

原因在于null必须用特殊的表达式来表示,分为 is null 和is not null,例如

select sname from test9 where sname is not null;