SELECT * FROM 表名;
SELECT * FROM student;
SELECT 字段1, 字段2, 字段3, ··· FROM 表名;
SELECT name, age FROM student;
SELECT 字段名1 AS 别名, 字段名2 AS 别名... FROM 表名;
SELECT 字段名 1 AS 别名, 字段名 2 AS 别名... FROM 表名 AS 表别名;
-- 使用别名
select name as 姓名,age as 年龄 from student;
-- 表使用别名
select st.name as 姓名,age as 年龄 from student as st
SELECT DISTINCT 字段名 FROM 表名;
-- 查询学生的年龄
select age from student;
-- 去掉重复的记录
select distinct age from student;
SELECT 列名 1 + 固定值 FROM 表名;
SELECT 列名 1 + 列名 2 FROM 表名;
注意: 参与运算的必须是数值类型
准备数据:添加数学,英语成绩列,给每条记录添加对应的数学和英语成绩,查询的时候将数学和英语的成绩相
加
select * from student;
-- 给所有的数学加 5 分
select math+5 from student;
-- 查询 math + english 的和
select * from student;
select *,(math+english) as 总成绩 from student;
-- as 可以省略
select *,(math+english) 总成绩 from student;
如果没有查询条件,则每次查询所有的行。实际应用中,一般要指定查询的条件。对记录进行过滤。
SELECT 字段名 FROM 表名 WHERE 条件;
流程: 取出表中的每条数据,满足条件的记录就返回,不满足条件的记录不返回
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);
-- 查询 math 分数大于 80 分的学生
select * from student3 where math>80;
-- 查询 english 分数小于或等于 80 分的学生
select * from student3 where english <=80;
-- 查询 age 等于 20 岁的学生
select * from student3 where age = 20;
-- 查询 age 不等于 20 岁的学生,注:不等于有两种写法
select * from student3 where age <> 20;
select * from student3 where age != 20;
逻辑运算符 | 说明 |
---|---|
and 或 && | 与,SQL中建议使用前者,后者并不通用。 |
or 或 || | 或 |
not 或 ! | 非 |
-- 查询 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 表名 WHERE 字段 in (数据 1, 数据 2...);
in 里面的每个数据都会作为一次条件,只要满足条件的就会显示
-- 查询 id 是 1 或 3 或 5 的学生
select * from student3 where id in(1,3,5);
-- 查询 id 不是 1 或 3 或 5 的学生
select * from student3 where id not in(1,3,5);
-- 表示从值 1 到值 2 范围,包头又包尾
BETWEEN 值 1 AND 值 2
比如:age BETWEEN 80 AND 100 相当于: age>=80 && age<=100
-- 查询 english 成绩大于等于 75,且小于等于 90 的学生
select * from student3 where english between 75 and 90;
LIKE 表示模糊查询
SELECT * FROM 表名 WHERE 字段名 LIKE '通配符字符串';
通配符 | 说明 |
---|---|
% | 匹配任意多个字符串 |
_ | 匹配一个字符 |
-- 查询姓马的学生
select * from student3 where name like '马%';
-- 查询姓名只有马的同学
select * from student3 where name like '马';
-- 查询姓名中包含'德'字的学生
select * from student3 where name like '%德%';
-- 查询姓马,且姓名有两个字的学生
select * from student3 where name like '马_';
SELECT 字段名 FROM 表名 WHERE 字段=值 ORDER BY 字段名 [ASC|DESC];
-- ASC : 升序,默认值
-- DESC :降序
-- 查询所有数据,使用年龄降序排序
select * from student order by age desc;
组合排序的语法:
SELECT 字段名 FROM 表名 WHERE 字段=值 ORDER BY 字段名 1 [ASC|DESC], 字段名 2 [ASC|DESC];
-- 查询所有数据,在年龄降序排序的基础上,如果年龄相同再以数学成绩升序排序
select * from student order by age desc, math asc;
SQL中的聚合函数 | 作用 |
---|---|
max(列名) | 求这一列的最大值 |
min(列名) | 求这一列的最小值 |
avg(列名) | 求这一列的平均值 |
count(列名) | 统计这一列有多少条记录 |
sum(列名) | 对这一列求总和 |
SELECT 聚合函数(列名) FROM 表名;
-- 查询学生总数
select count(id) as 总人数 from student;
select count(*) as 总人数 from student;
select ifnull(id, 0) from student;
select count(ifnull(id,0)) from student;
-- 查询年龄大于 20 的总数
select count(*) from student where age > 20;
-- 查询数学成绩总分
select sum(math) from student;
-- 查询数学成绩平均分
select avg(math) from student;
-- avg(math) = sum(math) / count(math)
-- 查询数学成绩最高分
select max(math) from student;
-- 查询数学成绩最低分
select min(math) from student;
SELECT 字段 1,字段 2... FROM 表名 GROUP BY 分组字段 [HAVING 条件];
将分组字段结果中相同内容作为一组,如按性别将学生分成 2 组。
GROUP BY 将分组字段结果中相同内容作为一组,并且返回每组的第一条数据,所以单独分组没什么用处。
分组的目的就是为了统计,一般分组会跟聚合函数一起使用。
-- 按性别进行分组,求男生和女生数学的平均分
select sex, avg(math) from student3 group by sex;
select sex, count(*) from student3 group by sex;
select sex, count(*) from student3 where age > 25 group by sex ;
SELECT sex, COUNT(*) FROM student3 WHERE age > 25 GROUP BY sex WHERE COUNT(*) >2;
-- 对分组查询的结果再进行过滤
SELECT sex, COUNT(*) FROM student3 WHERE age > 25 GROUP BY sex having COUNT(*) >2;
语句 | 作用 |
---|---|
where | 1) 对查询结果进行分组前,将不符合 where 条件的行去掉,即在分组之前过滤数据,即先过滤再分组。 |
where | 2) where 后面不可以使用聚合函数 |
having | 1) having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,即先分组再过滤。 |
having | 2) having 后面可以使用聚合函数 |
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);
LIMIT 是限制的意思,所以 LIMIT 的作用就是限制查询记录的条数。
SELECT *|字段列表 [as 别名] FROM 表名 [WHERE 子句] [GROUP BY 子句] [HAVING 子句] [ORDER BY 子句] [LIMIT 子句];
limit offset, length;
-- offset : 起始行数,从0开始计数,如果省略,默认是0
-- length :返回的行数
-- 查询学生表中数据,从第 3 条开始显示,显示 6 条。
select * from student3 limit 2,6;
分页: 比如我们登录京东,淘宝,返回的商品信息可能有几万条,不是一次全部显示出来。是一页显示固定的条数。 假设我们每页显示 5 条记录的方式来分页。
-- 如果第一个参数是 0 可以省略写:
select * from student3 limit 5;
-- 最后如果不够 5 条,有多少显示多少
select * from student3 limit 10,5;