select [distinct] [*] [列名1,列名2] from 表名 [where 条件]
distinct 去除重复的数据
select 选择显示哪些列的内容
--商品分类: 手机数码,鞋靴箱包
1.分类的ID
2.分类名称
3.分类描述
1,2
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,'馋嘴零食','瓜子花生,八宝粥,辣条');
--所有商品
1.商品ID
2.商品名称
3.商品的价格
4.生产 日期
5.商品分类ID
商品和商品分类:所属关系
insert into product values(null,'小米mix4',998,null,1);
insert into product values(null,'锤子',2888,null,1);
insert into product values(null,'阿迪王',99,null,2);
insert into product values(null,'老村长',88,null,3);
insert into product values(null,'劲酒',35,null,3);
insert into product values(null,'小熊饼干',1,null,4);
insert into product values(null,'卫龙辣条',1,null,5);
insert into product values(null,'旺旺饼干',1,null,5);
--简单查询
--查询所有的商品
select * from product;
--查询商品名称和商品价格
select pname,price from product;
---别名查询,as关键字,as关键字是可以省略
--表别名: select p.pname,p.price from product p; (主要是用在多表查询)
select p.pname,p.price from product as p;
--列别名: select pname as 商品名称, price as 商品价格 from product;
select pname as 商品名称,price as 商品价格 from product;
省略as关键字
select pname 商品名称,price 商品价格 from product;
---去掉重复的值
--查询商品所有的价格
select price from product;
select distinct price from product;
--select运算查询 : 仅仅在查询结果上做了运算 + - * /
select *,price*1.5 from product;
select *,price*1.5 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;
--逻辑运算: and ,or,not
--查询出商品价格 小于35 或者商品价格 大于900
select * from product where price < 100 or price > 900;
--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.查询所有的商品,按价格进行降序排序(asc-升序 desc-降序);
select * from product order by price desc;
--2.查询名称有 小 的商品,按价格升序排序
--查询名称有小的商品
select * from product where pname like '%小%';
--进行排序得出结果
select * from product where pname like '%小%' order by price asc;
--聚合函数:
sum() : 求和
avg() : 求平均值
count(): 统计数量
max(): 最大值
min(): 最小值
--1.获得所有商品价格的总和
select sum(price) from product;
--2.获得所有商品的平均价格
select avg(price) from product;
--3.获得所有商品的个数
select count(pid) from product;
--注意: where条件后面不能接聚合函数
--分组: group by
--1.根据cno字段分组,分组后统计商品的个数
select cno,count(*) from product group by cno;
--2.根据cno分组,分组统计每组商品的平均价格 并且商品平均价格 > 60
select cno,avg(price) 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