select [distinct] [*] [列名,列名2] from 表名 [where 條件]
distinct: 去除重複的數據
--商品分類: 手機數碼, 皮靴箱包
1.分類ID
2.分類名稱
3.分類描述
Create table category (
cid int primary key auto_increment,
cname varchar(10),
cdesc varchar(30)
);
insert into category values(null, '手機數碼','電子產品,黑馬生產');
insert into category values(null, '皮靴箱包','江南皮革廠親情打造');
insert into category values(null, '香菸酒水','衡水老白乾');
insert into category values(null, '酸奶餅乾','君樂寶乳業');
insert into category values(null, '饞嘴零食','花生瓜子八寶粥');
select * form category;
select cname,cdesc from category;
--所有商品
1.商品ID
2.商品名稱
3.商品價格
4.商品數量
5.生產日期
6.商品分類ID
商品和商品分類: 所屬關係
create table product(
pid int primary key auto_increment,
pname varchar(10) ,
price double,
pdate timestamp, (時間戳,默認當前時間)
cno int
);
insert into product values(null, '小米mix8',998,null,1);
insert into product values(null, '錘子',2888,null,1);
insert into product values(null, '阿迪王',99,null,2);
insert into product values(null, '老白乾',598,null,3);
insert into product values(null, '敬酒',298,null,3);
insert into product values(null, '小熊餅乾',1,null,4);
insert into product values(null, '衛龍辣條',3,null,5);
--簡單查詢
--查詢所有商品信息
select * from product;
--查詢商品名稱和商品價格
select pname,price from product;
--別名查詢, as的關鍵字, as 關鍵字可以省略
--表別名: select p.pname , p.price from product p; (主要用於奪標查詢)
select p.pname as 商品名稱 , p.price as 商品價格 from product as p;
select p.pname 商品名稱 , p.price 商品價格 from product p;
--去掉重複的值
--查詢所有商品的價格
select price from product;
select distinct price from product;
--select 運算查詢 僅僅在查詢結果上做了運算處理 + - * /
select * , price *0.9 as 折後價 from product;
--條件查詢( where關鍵字 )
指定條件,確定要操作的紀律
--查詢商品價格大於60元的所有商品信息
select * from product where price > 60;
--where 後的條件寫法
--關係運算發: > >= < <= != <>
<>:不等於: 標準SQL語法
!=: 不等於: 非標準SQL語法
--查詢商品價格不等於88的所有商品
select * from product where price != 88;
select * from product where price <> 88;
--查詢商品價格再 10 到 100 之間
select * from product where price >= 10 and price <= 100;
between ...and...
select * from product where price between 10 and 100;
--查詢商品價格大於100 或者 商品價格大於10
select * from product where price > 100 or price < 10;
--like : 模糊查詢
- : 代表是一個字符
% : 代表多個字符
--查詢出名字中帶 有餅的所有商品 '%餅%'
select * from product where pname like '%餅%';
--查詢第二個名字是 酒的所有商品 '_酒%'
select * from product where pname like '_酒%';
--in 在某個範圍中獲得值
--查詢出商品分類 ID 在 1,4,5 裏面的所有商品
select * from product where cno in (1,4,5);
--排序查詢: order by 關鍵字
asc: ascend 升序 (默認)
desc: descend 降序
--0.查詢所有商品按照價格進行排序
select * from product order by price;
--1.查詢所有的商品,按照價格進行降序 排序
select * from product order by price desc;
--2.查詢名稱有 幹 的商品 ,按照價格降序排序
select * from product where pname like '%幹%' order by price desc;
--聚合函數:
sum() 求和
avg() 求平均值
count() 統計數量
max() 最大值
min() 最小值
--1.獲得所有的商品價格的總和
select sum(price) from product ;
--2.獲得所有商品的平均價格
select avg(price) from product;
--3.獲得所有商品的個數
select count(*) from product;
--注意: where條件後面不能接聚合函數
--查出商品價格大於平均價格的所有商品信息
select * from product where price > avg(price); ×
查出所有商品
select * from product;
大於
平均價格
select avg(price) from product;
select * from product where price > (select avg(price) from product);
--分組: group by
--1.根據cno字段分組,分組後統計商品id
select cno,count(*) from product group by cno;
--2.根據cno分組,分組統計每組的平均價格,並且商品平均價格 > 60
select avg(price),cno from product group by cno
having avg(price) > 60;
--having 關鍵字 可以接聚合函數的 出現在分組之後
--where 關鍵字 他是不可以接聚合函數的 , 出現在分組之後
--編寫順序
select ... from ... where ... group by ... having ... order by
--執行順序
from ... where ... group by ... having select ... order by
IFNULL(expr1,expr2)
當expr1不爲空返回expr1,否則返回exp2。
例如:IFNULL(score,0)如果score不爲空返回score,否則返回0。