DQL-基础查询和DQL-条件查询
DQL-基础查询
多个字段的查询
select 字段名1,字段名2,字段名3... from 表名;
注意:
如果查询所有字段,则可以使用*来代替字段列表
select * from 表名;
去除重复:
distinct
-- 去除重复的结果集
SELECT DISTINCT address from student
计算列
一般可以使用四则运算计算一些列的值,(一般只会进行数值 型的计算)
ifnull(表达式1,表达式2):null参与的运算。计算结果都为null
表达式1:哪个字段需要判断是否为null
如果该字段为null后的替换值
-- 计算math和english的分数之和
SELECT NAME,math,english,math + english from student
-- 如果有null参与的运算,那么计算的结果都为null;
SELECT NAME,math,english,math + ifnull(english,0) from student
起别名
as:as也可以省略
-- 起别名
SELECT NAME,math,english,math + ifnull(english,0) as 总分 from student
SELECT NAME,math 数学,english 英语,math + ifnull(english,0) 总分 from student
DQL-条件查询
where子句后跟条件
运算符:
>,<,<=,>=,=,<>
BETWEEN . . . AND
IN (集合)
LIKE
IS NULL
AND 或 &&
OR 或 ||
NOT 或 !
-- 查询年龄大于20岁 SELECT * FROM student where age>20; SELECT * FROM student where age>=20; -- 查询年龄等于20岁 SELECT * FROM student where age = 20 -- 查询年龄不等于20岁 SELECT * FROM student where age !=20 SELECT * FROM student where age <>20 -- 查询年龄大于等于20小于等于30 SELECT * FROM student where age>=20 && age <=30 SELECT * FROM student where age>=20 and age <=30 SELECT * FROM student where age BETWEEN 20 and 30 -- 查询年龄22岁,18岁,15岁的人的信息 SELECT * FROM student where age = 22 or age = 18 or age=25 SELECT * FROM student where age IN (22,18,25) -- 查询英语成绩为null SELECT * FROM student where english = null -- 这个语句是错误的,null值不能使用=(!=)判断 SELECT * FROM student where english is null -- 查询英语成绩不为null SELECT * FROM student where english is not null

浙公网安备 33010602011771号