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复制代码
插入数据mysql
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复制代码
①where expressionsql
用法:expression为真,则该行取出express
运用场合bash
各类条件查询场合,如按学号查学生,按价格查商品,按发布时间查新闻等函数
②select 5种子句 之where经常使用运算符ui
③select 5种子句 之where 匹配spa
like 模糊匹配 % 通配任意字符 _ 通配单一字符3d
select goods_id,cat_id,goods_name,shop_price from goods where shop_price <= 100;复制代码
select goods_id,cat_id,goods_name,shop_price from goods where cat_id in (4, 11);复制代码
select goods_id,cat_id,goods_name,shop_price from goods where shop_price between 100 and 500;复制代码
select goods_id,cat_id,goods_name,shop_price from goods where goods_name like '诺基亚%';复制代码
select goods_id,cat_id,goods_name,shop_price from goods where goods_name like '诺基亚n__';
复制代码
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)unsignedcode
unsigned字段相减,不能为负数,我不知道为何,mysql就是不行。。。cdn
解决方案,把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);复制代码
①select 5种子句 之group与统计函数
max : 求最大 min : 求最小 sum : 求总和 avg : 求平均 count:求总行数
②select 5种子句 之group介绍
group by
做用:把行 按 字段 分组
语法:group by col1,col2,...colN
运用场合
常见于统计场合,如按栏目计算帖子数, 统计每一个人的平均成绩等.
select goods_name,sum(goods_number) from goods group by goods_name;复制代码
①select 5种子句 之having介绍
having 与where异同点
having 与where相似,可筛选数据
where后的表达式怎么写,having就怎么写
where针对表中的列发挥做用,查询数据
having针对查询结果中的列发挥做用,筛选数据
select cat_id,sum(goods_number*shop_price) as sumMoney from goods group by cat_id
having sumMoney > 20000复制代码
# 建表
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;复制代码
①select 5种子句 之order排序
Order by 排序功能
按一个或多个字段对查询结果进行排序
用法:order by col1,col2,col3
知识点的运用场合描述
各类排序场合,如新闻按点击量排序,商品按价格排序等
默认排序:升续排列
# asc表明升序,desc表明降序
select goods_id,cat_id,goods_name,shop_price
from goods
order by cat_id asc, shop_price desc复制代码
①select 5种子句 之limit 介绍
Limit 限制条数
limit [offset,] N,限制结果取N条
用法: limit [偏移量,],取出条目
知识点的运用场合描述
分页应用中最为典型,如第1页取1-20条,第2页取21-40条.
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条数据。。。。。。。。。。。。。。。。。。。。。复制代码
一、首先查出每一个栏目下的goods_id
select max(goods_id),cat_id from goods
group by cat_id
二、把上面的查询结果做为where的子句
select goods_id, goods_name from goods
where goods_id in
(select max(goods_id) from goods group by cat_id);复制代码
一、首先查出每一个栏目下商品价格从高到底排序的结果
select goods_id, goods_name from goodsorder by cat_id, shop_ price desc
二、把上面的查询结果做为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;复制代码
# 须要再建一张表,结合上面的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手机'),
('双模手机'),
('手机配件'),
('充电器'),
('耳机'),
('电池'),
('读卡器和内存'),
('充值卡'),
('小灵通/固话充值卡'),
('移动手机充值卡'),
('联通手机充值卡');
复制代码
select cat_id,cat_name from category where exists
(select * from goods where goods.cat_id = category.cat_id);复制代码
# 咱们先创建一张表
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;复制代码