select [distinct] [ * ] [列名1,列名2] from 表名 [where 条件]
distinct : 去除重复的数据
select distinct sex from student; -- 男/女
--商品分类:手机数码,鞋靴箱包
1.分类的ID
2.分类名称
3.分类描述
create table category(
cid int primary key auto_increment,
cname varchar(10),
cdesc varchar(31)
);
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
商品和商品的分类:所属关系
create table product(
pid int primart key auto_increment,
pname varchar(10),
price double,
pdate timestamp,
con int
);
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,'安慕希',78,null,4);
insert into product values(null,'卫龙',1,null,5);
insert into product values(null,'小熊饼干'null,5);
insert into product values(null,'三只松鼠',132,null,5);
--简单查询
--查询所有的商品:
select * from product;
--查询商品名称和商品价格
select pname,price from product;
--别名查询,as 关键字,as关键字是可以省略的
--表别名:selece p.pname,p.price from product p;(主要用在多表查询);
--列别名:select pname as 商品名称,price as 商品价格 from product;
select pname as 商品名称,price as 商品价格 from product;
省略关键字
select pname 商品名称,price 商品价格 from product;
--去 掉重复的值
--查询商品所有的价格
select prict from product;
select distinct price product;
--select运算查询:仅仅在查询结果上做了运算 + - * /
select *,price*1.5 from product;
select *,price*1.5 as 折后价 from product;
select *,price*0.9 from product;
--条件查询(where关键字)
指定条件,确定要操作的记录
--查询商品价格>60元的所有商品信息
select * from product where price > 60;
--where 后的条件写法
--关系运算符: > >= < <= = != <>
<> :不等于 : 标准SQL语法
!= :不等于: 非标准SQL语法
--查询商品价格不等于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 , out
--查询出商品价格小于35 或者商品价格大于900
select * from product where price < 35 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); --查询出商品分类ID不在 3 里面的所有商品
select * from product where con not in (3);
--排序查询: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;
--组合排序 :先按第一个字段进行排序,如果第一个字段相同,才按第二个字段进行排序,
--查询价格升序,如果价格相同,按分组(con)降序排序
select * from product order by price asc, con desc;
--聚合函数:
sum() 求和
avg() 求平均值
count():统计数量
max:最大值
min():最小值
--1.获得所有商品价格的总和:
select sum(price) from product;
--2.获得所有商品的平平均价格
select avg(price) from product;
--3.获得所有的商品的个数,值为NULL跳过。
select count(*) from product;
--4.获得所有商品的个数,如果值为 NULL 时也计入统计。
select count(ifnull(price,0)) from product;
--注意:where 条件后面不能接聚合函数
select * from product where price > avg(price);
--查出商品价格大于平均价格的所有商品
查出所有商品
select * from product;
大于
平均价格
select avg(price) from product;
select * from product where price > avg(price);
转换后的格式为: --
select * from product where price >( select avg(price) from product );
--分组:group by
--1.根据con字段分组,分组后统计商品个数
select cno,count(*)
from product grop by cno;
--2.根据con分组,分组统计每组商品的平均价格 , 并且商品平均价格 > 60
select avg(price)
from product group by cno
having avg(price) > 60;
--3.根据con分组,分组统计每组商品的个数,并且商品的价格要大于50;
select count(pid),con from product where price > 50 group by con;
--##having关键字可以接聚合函数的 出现在分组之后
-- having与where的区别
having是在分组后对数据进行过滤.
where是在分组前对数据进行过滤
having后面可以使用聚合函数
where后面不可以使用聚合函数
--限制:limit语句 LIMIT 是 限制 的意思,所以 LIMIT 的作用就是限制查询记录的条数。
--1.查询所有商品,从第三条开始显示,显示六条(如果不够六条,有多少显示多少。如果第一个参数是0可以简写)
select * from product limit 2,6;
--编写顺序
-- s..f..w..G..H..o
select .. from .. where .. group by .. having .. order by
--执行顺序
F..W..G..H..S..O
from .. where ..group by .. having .. select .. order by
得到这个表 .. 条件判断 .. 还是这个表再进行分组 .. 再做一个条件筛选 .. 最后进行控制显示 .. 显示的结果再去排序