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;複製代碼