emp 员工表
create table emp(
empno int primary key,
ename nvarchar(45),
job nvarchar(45),
mgr int,
hiredate datetime,
sal numeric(8,2),
comm numeric(8,2),
deptno int
);
查询实例1
# 查询所有的列
select * from emp;
# 查询指定的列
select ename,job from emp;
# 去掉重复的项
select distinct deptno from emp;
# 两个字段都重复才去掉
select distinct deptno,job from emp;
# 算术表达式 ifnull(1,2) 如果为空取后面的 如果不为空取前面的
select ename,sal*12+ifnull(comm,0)*12 from emp;
# 查询工资大于2000小于3000的的员工姓名和工资
select ename,sal from emp where sal between 2000 and 3000;
# 查询1981-4 之后入职的员工 要将日期 '' 起来
select * from emp where hiredate>'1981-04-01';
# 查询员工平均工资
select avg(sal) from emp;
# 查询有多少员工拿奖金
select count(comm) from emp;
# 只要有一个字段不为空则统计,相当于统计所有员工,不建议
select count(*) from emp;
# 查询最低工资为多少
select min(sal) from emp;
# 查询最高工资为多少
select max(sal) from emp;
# 查询最高工资的员工姓名及工资
select ename,sal from emp where sal=(select max(sal) from emp);
# 查询首字母为s的员工姓名和工资 要''起来,%代表0或多个字符
select ename,sal from emp where ename like 's%';
# 查询名字中有o的员工
select ename,sal from emp where ename like '%o%';
# 查询名字中第三个字符为o的员工 下划线_表示单个字符
select ename,sal from emp where ename like '__o%';
# 查询员工编号为 7499 7782 7839的员工薪水 in()
select empno,ename,sal from emp where empno in(7499,7782,7839);
# 查询没有奖金的员工姓名,在遇到null时不能用 =
select ename from emp where comm is null;
# 查询工资高于800或者岗位为manager的员工,同时满足他们的姓名首写字母为j
select ename,sal,job from emp where (sal>800 or job='manager') and ename like 'j%';
# 按照工资从升序显示员工信息 desc 降序 默认升序asc
select * from emp order by sal asc;
# 统计年薪,并按照年薪降序排序 as 取别名
select ename,(sal+ifnull(comm,0))*12 as nx from emp order by nx desc;
# 显示每个部门的平均工资与最高工资 group by 按 部门分组统计
select deptno,avg(sal),max(sal) from emp group by deptno;
# 显示平均工资低于2000的部门编号及其工资 having 在select xx运行完之后进行筛选
select deptno,avg(sal) as a from emp group by deptno having a<2000;
# 查询员工信息按部门降序,工资升序排列 满足部门降序的情况,按工资升序排列
select ename,deptno,sal from emp order by deptno desc,sal asc;
# 查询奖金不为空的员工信息
select * from emp where comm is not null;
# 查询部门编号不等于30的员工信息
select * from emp where deptno != 30;
# 插入数据
INSERT INTO `d_product` VALUES ('1', '苹果', '5000', 'c001');
INSERT INTO `d_product` VALUES ('2', 'vivo', '3000', 'c001');
INSERT INTO `d_product` VALUES ('3', '华为', '5000', 'c001');
INSERT INTO `d_product` VALUES ('4', 'oppo', '1000', 'c001');
INSERT INTO `d_product` VALUES ('5', '真维斯', '200', 'c002');
INSERT INTO `d_product` VALUES ('6', 'only', '440', 'c002');
INSERT INTO `d_product` VALUES ('7', '圣迪奥', '800', 'c002');
INSERT INTO `d_product` VALUES ('8', 'vero moda', '800', 'c003');
INSERT INTO `d_product` VALUES ('9', '相宜本草', '200', 'c003');
INSERT INTO `d_product` VALUES ('10', '卫龙辣条', '5', NULL);
INSERT INTO `d_product` VALUES ('11', '好想你枣', '56', NULL);
INSERT INTO `d_product` VALUES ('12', '香飘飘奶茶', '3', NULL);
INSERT INTO `d_product` VALUES ('13', '小浣熊干脆面', '1', NULL);
# 查询所有的列
select pid,pname,price,category_id from d_product;
select * from d_product;
# 查询部分列
select pid,pname from d_product;
# 给列起别名
select pid as '商品id',pname as '商品名称' from d_product;
# 给表起别名
select pid,a.pname from d_product a;
# 列为表达式
select pid,pname,price*10,category_id from d_product;
# 去除重复项 distinct 如果查2个 则要求两个都相同才去重
select distinct price from d_product;
# 查询价格为800的商品
select * from d_product where price=800;
# 查询价格不是800的商品
select * from d_product where price!=800;
select * from d_product where price<>800;
select * from d_product where not(price=800);
# 查询商品价格大于800的商品信息
select * from d_product where price>800;
# 查询商品价格在800到2000之间的所有商品
select * from d_product where price>=800 and price<=2000;
select * from d_product where price between 800 and 2000;
# 查询商品价格是200或者800的所有商品
select * from d_product where price=200 or price=800;
select * from d_product where price in (200,800);
# 查询含有'0' 字的所有商品
select * from d_product where pname like '%o%';
# 查询以'o'开头的所有商品
select * from d_product where pname like 'o%';
# 查询第四个字为'o'的所有商品
select * from d_product wher pname like '___o%';
# 没有被分类的商品,即没有category_id的
select * from d_product where category_id is null;
# 查询被分类的商品
select * from d_product where category_id is not null;
# 筛选出价格在800以上,同时包含'o'字符的商品id和名称、价格
select pid,pname,price from d_product where price>800 and pname like '%o%';
# 使用价格排序--降序
select * from d_product order by price desc;
# 在价格降序的基础上,以分类排序--降序
select * from d_product order by price desc,category_id desc;
# 显示商品的价格,去重并排序--降序
select distinct price from d_product order by price desc;
# 默认按照主键升序排列
select * from d_product;
# 采用order by关键字
# 按照价格升序 默认为升序
select * from d_product order by price asc;
# 结合列筛选和行筛选 商品名含有o 按价格降序
select pname,price from d_product where pname like '%o%' order by price desc;
# 统计列名个数 count(列名)
# 统计价格在800以上一共有多少商品
select count(*) as total from d_product where price>=800;
# 统计一共有几种价格
select count(distinct price) from d_product;
# sum计算和值
# 获取价格最大的商品价格
select max(price) from d_product;
# min获取最小值
# 获取价格最小的商品价格
select min(price) from d_product;
# avg获取平均值
# 获取所有商品的平均价格
select avg(price) from d_product;
# 分组 group by 分组字段 having 分组后过滤条件
# having 在分组后进行过滤 where在分组前进行过滤
# having 后面可以使用分组函数(统计函数) where不行
# 分组 最好和聚合函数结合使用
# 按照价格分组
select price from d_product group by price;
# 按照类别id分组 该类名的商品个数,平均价格,类别
select count(*),avg(price),category_id from d_product group by category_id;
# 进行筛选后再分组 对非空的categor_id进行分组 注意where的位置
select count(*),avg(price),category_id from d_product where category_id is not null group by category_id;
# 先分组再筛选 筛选出平均价格在500及以上的商品分组
select count(*),avg(price),category_id from d_product group by category_id having avg(price)>=500;