一、简单查询
-- DQL 语句
-- 简单查询
-- 查询所有数据
select * from student;
select id,name,birthday,age from student;
-- 查询指定列
select id,name from student ;
-- 别名查询 as可以省略,可以查询多列
select id as stu_id,name stu_name from student;
-- 清除重复值
select distinct name from student;
select distinct age from student;
-- 去掉名字和年龄都相同的,留下一行
select distinct name,age from student ;
-- 查询结果参与运算
-- null做任何运算之后还是null
select id, name,math+english total from student;
-- ifnull函数
select id,name,math+ifnull(english,0) total from student;
二、条件查询
2.1准备数据
-- 准备数据
CREATE TABLE student3 (
id int,
name varchar(20),
age int,
sex varchar(5),
address varchar(100),
math int,
english int
);
INSERT INTO student3(id,NAME,age,sex,address,math,english) VALUES (1,'马云',55,'男','杭州',66,78),(2,'马化腾',45,'女','深圳',98,87),(3,'马景涛',55,'男','香港',56,77),(4,'柳岩',20,'女','湖南',76,65),(5,'柳青',20,'男','湖南',86,NULL),(6,'刘德华',57,'男','香港',99,99),(7,'马德',22,'女','香港',99,99),(8,'德玛西亚',18,'男','南京',56,65);
2.2各种查询
-- 条件查询
-- 比较运算符 >,<,>=,<=,!=,<>
-- 查询math分数大于80分的学生
select *from student3 where math>80;
-- 查询english分数小于或等于80分的学生
select * from student3 where math >80;
-- 查询age等于20岁的学生
select * from student3 where age = 20;
-- 查询age不等于20岁的学生
select * from student3 where age != 20;
select * from student3 where age <>20;
-- 逻辑运算符
-- 查询age大于35且性别为男的学生(两个条件同时满足)
select * from student3 where age>35 and sex='男';
-- 查询age大于35或性别为男的学生(两个条件其中一个满足
select *from student3 where age>35 or sex='男';
-- 查询id是1或3或5的学生
select *from student3 where id=1 or id=3 or id=5;
select *from student3 where id in(1,3,5);
-- 查询id不是1或3或5的学生
select *from student3 where id not in(1,3,5);
-- 范围
-- 查询english成绩大于等于75,且小于等于90的学生
select *from student3 where english>=75 and english<=90;
select *from student3 where english between 75 and 90;
-- like
-- 查询姓马的学生
select *from student3 where name like '马%';
-- 查询姓名中包含'德'字的学生
select *from student3 where name like '%德%';
-- 查询姓马,且姓名有三个字的学生
select *from student3 where name like '马__';
-- null值查询
select *from student3 where english is null;
select *from student3 where english is not null;
2.3排序
-- 单列排序
-- 查询所有数据,使用年龄降序排序
select * from student3 order by age desc;
-- 根据列别名排序
select id,name ,age stu_age from student3 order by stu_age;
-- 根据列序号排序
select id,name ,age stu_age from student3 order by 3;
-- 组合排序
-- 查询所有数据,在年龄降序排序的基础上,如果年龄相同再以数学成绩降序排序
select * from student3 order by age desc,math desc;
2.4聚合函数
count 个数
sum 总数
avg 平均数
max 最大值
min 最小值
-- 聚合函数
select count(*) from student3;
-- 查询年龄大于40的总数
select count(*) from student3 where age>40;
-- 查询数学成绩总分
select sum(math) from student3;
-- 查询数学成绩平均分
select avg(math) from student3;
-- 查询数学成绩最高分
select max(math) from student3;
-- 查询数学成绩最低分
select min(math) from student3;
-- 查询英语成绩最低分
select min(english) from student3;
-- 查询英语成绩总分
select sum(english) from student3;
select sum(ifnull(english,0)) from student3;
-- 查询英语成绩平均分
select avg(english) from student3;
select avg(ifnull(english,0)) from student3;
2.5单行函数
ifnull()
sysdate()
now()
curdate()
curtime()
-- 如果来自自己创建的表,有多少条记录就返回多少条时间
select sysdate() from student3;
#dual 虚表
--2019-11-25 20:30:50 -- 年月日,时分秒
select sysdate() from dual;
--2019-11-25 20:30:38 -- 年月日,时分秒
select now();
--2019-11-25 -- 年月日
select curdate();
--20:31:30 -- 时分秒
select curtime();
2.6分组
-- 按性别分组
select sex from student3 group by sex;
-- 查询男女各多少人
select sex ,count(*) from student3 group by sex;
-- 查询年龄大于25岁的人,按性别分组,统计每组的人数
select sex,count(*) from student3 where age>25 group by sex;
-- 查询年龄大于25岁的人,按性别分组,统计每组的人数,并只显示性别人数大于2的数据
select sex ,count(*) from student3 where age>25 group by sex having count(*)>2;
2.7limit
INSERT INTO student3(id,NAME,age,sex,address,math,english) VALUES
(9,'唐僧',25,'男','长安',87,78),
(10,'孙悟空',18,'男','花果山',100,66),
(11,'猪八戒',22,'男','高老庄',58,78),
(12,'沙僧',50,'男','流沙河',77,88),
(13,'白骨精',22,'女','白虎岭',66,66),
(14,'蜘蛛精',23,'女','盘丝洞',88,88);
-- 查询学生表中数据,从第三条开始显示,显示6条
select * from student3 limit 2,6;
-- 显示前五条:如果前面是0,可以省略
select * from student3 limit 0,5;
select * from student3 limit 5;
-- 如果没有那么多条,有多少显示多少,显示第二条到结束
select * from student3 limit 1,30;