MySQL-DQL

/*
DQL是指数据查询语言,英文全称是Data Query Language,用来对数据库中表的数据记录进行查询。

语法格式:
select
  [all|distinct]
  <目标列的表达式1> [别名],
  <目标列的表达式2> [别名]...
from <表名或视图名> [别名],<表名或视图名> [别名]...
[where<条件表达式>]
[group by <列名>
[having <条件表达式>]]
[order by <列名> [asc|desc]]
[limit <数字或者列表>];

简化版语法:
select *| 列名 from 表 where 条件
*/
-- 创建商品表:
create table product
(
    pid         int primary key auto_increment, -- 商品编号
    pname       varchar(20) not null,           -- 商品名字
    price       double,                         -- 商品价格
    category_id varchar(20)                     -- 商品所属分类
);
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 pn
from product;

-- 4.去掉重复值.
select distinct price
from product;

# 当所有列都相同时,去重
# select distinct *
# from product;

-- 5.查询结果是表达式(运算查询):将所有商品的价格+10元进行显示.
select pname, price + 10 as 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 >= 200
  and price <= 1000;

select *
from product
where price >= 200
          && price <= 1000;

select *
from product
where price between 200 and 1000;

-- 查询商品价格是200或800的所有商品
select *
from product
where price = 200
   or price = 800;

select *
from product
where price = 200
          || price = 800;

select *
from product
where price in (200, 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;

# 求最小值
select least(15, 48, 2, 45) min;
# 含null则不会进行比较,结果直接为null
select least(15, null, 2, 45) min;

# 求最大值
select greatest(15, 48, 89, 45) max;
# 含null则不会进行比较,结果直接为null
select greatest(15, 48, null, 45) max;

# 排序查询
# 价格降序
select *
from product
order by price desc;

# 价格降序的基础上,按分类降序
# 价格相同的情况下,按分类排序
select *
from product
order by price desc, category_id desc;

# 对去重后的价格进行降序排序
select distinct price
from product
order by price desc;

# 聚合查询
# count()	统计指定列不为NULL的记录行数;
# sum()	计算指定列的数值和,如果指定列类型不是数值类型,那么计算结果为0;
# max()	计算指定列的最大值,如果指定列是字符串类型,那么使用字符串排序运算;
# min()	计算指定列的最小值,如果指定列是字符串类型,那么使用字符串排序运算;
# avg()	计算指定列的平均值,如果指定列类型不是数值类型,那么计算结果为0

# 查询商品的总条数
select count(*)
from product;

# 查询价格大于200商品的总条数
select count(*)
from product
where price > 200;

# 查询分类为'c001'的所有商品的价格总和
select sum(price)
from product
where category_id = 'c001';

# 查询商品的最大价格
select max(price) max_price
from product;

# 查询商品的最小价格
select min(price) min_price
from product;

# 查询分类为'c002'所有商品的平均价格
select avg(price)
from product
where category_id = 'c002';

/*
1、count函数对null值的处理
如果count函数的参数为星号(*),则统计所有记录的个数。而如果参数为某字段,不统计含null值的记录个数。

2、sum和avg函数对null值的处理
这两个函数忽略null值的存在,就好象该条记录不存在一样。

3、max和min函数对null值的处理
 max和min两个函数同样忽略null值的存在。
*/

# 分组查询
# 统计各个分类商品的个数
# 如果要进行分组的话,则select之后只能出现分组字段和聚合函数
select category_id, count(*)
from product
group by category_id;

# 统计各个分类商品的个数,且只显示个数大于4个的商品信息
select category_id, count(*) cnt
from product
group by category_id
having cnt > 4
order by cnt;


# 查询练习
create table student
(
    id      int,
    gender  varchar(10),
    name    varchar(20),
    chinese int,
    english int,
    math    int
);
insert into student
values (1, '男', '张小明', 89, 78, 90);
insert into student
values (2, '男', '李进', 67, 53, 95);
insert into student
values (3, '男', '王五', 87, 78, 77);
insert into student
values (4, '男', '李一', 88, 98, 92);
insert into student
values (5, '男', '李来财', 82, 84, 67);
insert into student
values (6, '女', '张进宝', 55, 85, 45);
insert into student
values (7, '女', '黄蓉', 75, 65, 30);
insert into student
values (7, '女', '黄蓉', 75, 65, 30);

-- 查询表中所有学生的信息
select *
from student;

-- 查询表中所有学生的姓名和对应的英语成绩
select name, english
from student;

-- 过滤表中重复数据
select distinct *
from student;

-- 统计每个学生的总分
select name, chinese + english + math total_score
from student;

-- 在所有学生总分数上加10分特长分
select name, chinese + english + math + 10 total_score
from student;

-- 使用别名表示学生分数
select name, chinese chi, english eng, math
from student;

-- 查询英语成绩大于90分的同学
select *
from student
where english > 90;

-- 查询总分大于200分的所有同学
select *, chinese + english + math
from student
where chinese + english + math > 200;

-- 查询英语分数在 80-90之间的同学
select *
from student
where english >= 80
  and english <= 90;

select *
from student
where english between 80 and 90;
-- 查询英语分数不在 80-90之间的同学
select *
from student
where english < 80
   or english > 90;

select *
from student
where english not between 80 and 90;

-- 查询数学分数为89,90,91的同学
select *
from student
where math in (89, 90, 91);

select *
from student
where math = 89
   or math = 90
   or math = 91;

-- 查询所有姓李的学生英语成绩
select name, english
from student
where name like '李%';

-- 查询数学分80并且语文分80的同学
select *
from student
where math = 80
  and chinese = 80;

-- 查询英语80或者总分200的同学
select *
from student
where english = 80
   or chinese + english + math = 200;

-- 对数学成绩排序后输出
select *
from student
order by math desc;

-- 对总分排序后输出,然后再按从高到低的顺序输出
select *, chinese + english + math
from student
order by chinese + english + math desc;

-- 对姓李的学生成绩排序输出
select *, chinese + english + math
from student
where name like '李%'
order by chinese + english + math desc;

# 查询男生和女生分别有多少人,按人数降序排序,查出人数大于4的性别
select gender, count(*) cnt
from student
group by gender
having cnt > 4
order by cnt desc;


create table emp
(
    empno    int,         -- 员工编号
    ename    varchar(50), -- 员工名字
    job      varchar(50), -- 工作名字
    mgr      int,         -- 上级领导编号
    hiredate date,        -- 入职日期
    sal      int,         -- 薪资
    comm     int,         -- 奖金
    deptno   int          -- 部门编号
);

insert into emp
values (7369, 'smith', 'clerk', 7902, '1980-12-17', 800, null, 20);
insert into emp
values (7499, 'allen', 'salesman', 7698, '1981-02-20', 1600, 300, 30);
insert into emp
values (7521, 'ward', 'salesman', 7698, '1981-02-22', 1250, 500, 30);
insert into emp
values (7566, 'jones', 'manager', 7839, '1981-04-02', 2975, null, 20);
insert into emp
values (7654, 'martin', 'salesman', 7698, '1981-09-28', 1250, 1400, 30);
insert into emp
values (7698, 'blake', 'manager', 7839, '1981-05-01', 2850, null, 30);
insert into emp
values (7782, 'clark', 'manager', 7839, '1981-06-09', 2450, null, 10);
insert into emp
values (7788, 'scott', 'analyst', 7566, '1987-04-19', 3000, null, 20);
insert into emp
values (7839, 'king', 'president', null, '1981-11-17', 5000, null, 10);
insert into emp
values (7844, 'turner', 'salesman', 7698, '1981-09-08', 1500, 0, 30);
insert into emp
values (7876, 'adams', 'clerk', 7788, '1987-05-23', 1100, null, 20);
insert into emp
values (7900, 'james', 'clerk', 7698, '1981-12-03', 950, null, 30);
insert into emp
values (7902, 'ford', 'analyst', 7566, '1981-12-03', 3000, null, 20);
insert into emp
values (7934, 'miller', 'clerk', 7782, '1982-01-23', 1300, null, 10);

-- 1、按员工编号升序排列不在10号部门工作的员工信息
select *
from emp
where deptno != 10
order by empno;

-- 2、查询姓名第二个字母不是”A”且薪水大于800元的员工信息,按年薪降序排列
select *, (sal * 12 + ifnull(comm, 0)) year_sal
from emp
where ename not like '_a%'
  and sal > 800
order by year_sal desc;

-- 3、求每个部门的平均薪水
select deptno, avg(sal)
from emp
group by deptno;

-- 4、求各个部门的最高薪水
select deptno, max(sal)
from emp
group by deptno;

-- 5、求每个部门每个岗位的最高薪水
select deptno, job, max(sal)
from emp
group by deptno, job;

-- 6、求平均薪水大于2000的部门编号
select deptno, avg(sal) avg_sal
from emp
group by deptno
having avg_sal > 2000;

-- 7、将部门平均薪水大于1500的部门编号列出来,按部门平均薪水降序排列
select deptno, avg(sal) avg_sal
from emp
group by deptno
having avg_sal > 1500
order by avg_sal desc;

-- 8、选择公司中有奖金的员工姓名,工资
select ename, sal, comm
from emp
where comm is not null;

-- 9、查询员工最高工资和最低工资的差距
select max(sal) - min(sal)
from emp;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值