-- 一、数据准备
-- 1.创建数据库
create database if not exists MyTestdb;
use MyTestdb;
-- 2、创建商品表:
create table product(
pid int primary key auto_increment, -- 商品编号
pname varchar(20) not null , -- 商品名字
price double, -- 商品价格
category_id varchar(20) -- 商品所属分类
);
-- 3、添加数据
insert into product values(null,'海尔洗衣机',5000,'c001');
insert into product values(null,'美的冰箱',3000,'c001');
insert into product values(null,'格力空调',5000,'c001');
insert into product values(null,'九阳电饭煲',200,'c001');
insert into product values(null,'啄木鸟衬衣',300,'c002');
insert into product values(null,'恒源祥西裤',800,'c002');
insert into product values(null,'花花公子夹克',440,'c002');
insert into product values(null,'劲霸休闲裤',266,'c002');
insert into product values(null,'海澜之家卫衣',180,'c002');
insert into product values(null,'杰克琼斯运动裤',430,'c002');
insert into product values(null,'兰蔻面霜',300,'c003');
insert into product values(null,'雅诗兰黛精华水',200,'c003');
insert into product values(null,'香奈儿香水',350,'c003');
insert into product values(null,'SK-II神仙水',350,'c003');
insert into product values(null,'资生堂粉底液',180,'c003');
insert into product values(null,'老北京方便面',56,'c004');
insert into product values(null,'良品铺子海带丝',17,'c004');
insert into product values(null,'三只松鼠坚果',88,null);
-- 二、简单查询
-- 1.查询所有的商品.
select * from product;
-- 2.查询商品名和商品价格.
select pname,price from product;
-- 3.别名查询.使用的关键字是as(as可以省略的).
-- 3.1表别名:
select * from product as p;
select * from product p;
-- 3.2列别名:
select pname as '商品名',price '商品价格' from product;
-- 4.去掉重复值-distinct
select distinct price from product;
-- 5.查询结果是表达式(运算查询):将所有商品的价格+10元进行显示.
select pname,price+10 new_price from product;
-- 运算符
use MyTestdb;
-- 将所有商品价格上调10%
select pname,price * 1.1 new_price from product;
-- 查询商品名称为“海尔洗衣机”的商品所有信息:
select * from product where pname='海尔洗衣机';
-- 查询价格为800商品
select * from product where price = 800;
-- 查询价格不是800的所有商品
select * from product where price != 800;
select * from product where price <> 800;
select * from product where not(price = 800);
-- 查询商品价格大于等于60元的所有商品信息
select * from product where price >= 60;
-- 查询商品价格在200到1000之间所有商品
select * from product where price between 200 and 1000;
select * from product where price >=200 and price <=1000;
select * from product where price >=200 && price <=1000;
-- 查询商品价格是200或800的所有商品
select * from product where price in(200,800);
select * from product where price=200 or price =800;
select * from product where price=200 || price =800;
-- 查询含有'裤'字的所有商品
select * from product where pname like '%裤%'; -- %用来匹配任意字符
-- 查询以'海'开头的所有商品
select * from product where pname like '海%';
-- 查询第二个字为'蔻'的所有商品
select * from product where pname like '_蔻%'; -- 下划线匹配单个字符
-- 查询商品所属分类(category_id)为null的商品
select * from product where category_id is null;
-- 查询商品所属分类(category_id)不为null分类的商品
select * from product where category_id is not null;
-- 使用least求最小值
select least(10, 20, 30); -- 10
select least(10, null , 30); -- 如果求最小值时,有个值为null,则不会进行比较,结果直接为null
-- 使用greatest求最大值
select greatest(10, 20, 30);
select greatest(10, null, 30); -- 如果求最大值时,有个值为null,则不会进行比较,结果直接为null
-- 排序查询_asc(默认-升序);desc(降序)
-- 1.使用价格排序(降序)
select * from product order by price desc;
-- 2.在价格排序(降序)的基础上,以分类排序(降序)
select * from product order by price desc,category_id desc;
-- 3.显示商品的价格(去重复),并排序(降序)
select distinct price from product order by price desc;
-- 聚合函数
-- 1 查询商品的总条数
select count(pid) from product;
select count(*) from product;
-- 2 查询价格大于200商品的总条数
select count(pid) from product where price>200;
-- 3 查询分类为'c001'的所有商品的总和
select sum(price) from product where category_id='c001';
-- 4 查询商品的最大价格
select max(price) from product;
-- 5 查询商品的最小价格
select min(price) from product;
-- 6 查询分类为'c002'所有商品的平均价格
select avg(price) from product where category_id='c002';
-- 分组查询
-- 1 统计各个分类商品的个数