sql select语句
一、表格
varchar字符串 长度由输入的数字决定,数字来说长度不受限制,varchar范围长度:0-255(根据自己需求改)id要自动递增一般
1.一对一
直接查找就可以
2.表格一对多
推荐在多的表格加入外键,有冗余(多行),两张表一起做查找
2.多对多存复杂关系
三张表,单独一张表存复杂的关系
一对一存复杂关系,在哪个表合并都可以,合成一张表
二、常用sql查询方法
1.单表查询:
select 列名 from 表名 (大小写不做区分 完全一样)
select name from study
select name,age,sex from study
2.查找所有字段*
*是查找所有字段
select * from study
3.条件查询:
加条件 where子语句加行
字符串加上单/双引号都可以
select * from study where class = 1
select * from study where name = "小猫"
4.逻辑运算符:
使用 and、or、not、等逻辑运算符组合多个条件。
AND:所有条件都为真时结果才为真。
OR:至少一个条件为真时结果为真。
NOT:反转条件的布尔值。
select * from study where sex = "女" and class = 1
5.比较运算符
< > <= >= != <>不等于这两个都是
SELECT * FROM study WHERE age > 20; -- 查询 age 大于 20 的记录
SELECT * FROM study WHERE age < 20; -- 查询 age 小于 20 的记录
SELECT * FROM study WHERE age >= 20; -- 查询 age 大于等于 20 的记录
SELECT * FROM study WHERE age <= 20; -- 查询 age 小于等于 20 的记录
SELECT * FROM study WHERE age <> 20; -- 查询 age 不等于 20 的记录
SELECT * FROM study WHERE age != 20; -- 查询 age 不等于 20 的记录
6.范围查询:
介于两者之间 between...and...
in(2,4,6)在括号的2、4、6里留下,和not连用not in在括号里不要
select * from study where age between 20 and 25
select * from study where id in(2,4,6)
7.模糊查询like
前面加
%
(如%张
):表示匹配以 “张” 结尾的字符串。后面加
%
(如张%
):表示匹配以 “张” 开头的字符串。前后都加
%
(如%张%
):表示匹配包含 “张” 的字符串,无论 “张” 在开头、中间还是结尾。
select * from study where name like '%张_'
SELECT * FROM study WHERE name LIKE '%张%'; -- 查询 name 中包含 '张' 的记录
SELECT * FROM study WHERE name LIKE '张_'; -- 查询 name 以 '张' 开头且长度为 2 的记录
8.空值查询
使用 IS NULL 查询列值为 NULL 的记录
班级是空的,还没分配班级
不是空 is not null
SELECT * FROM study WHERE class IS NULL; -- 查询 class 为 NULL 的记录
SELECT * FROM study WHERE class IS NOT NULL; -- 查询 class 不为 NULL 的记录
9.排序子语句
order by 字段名 desc降序 asc升序(可省)
select * from study where sex = "女" order by age desc //按 age 列降序排列
10.限制查询 分页查询(分页)
页码page 一页大小*(前面那个值),pagesize
limit a,b // a表示索引值(与id没关系) b限制查询的个数
limit b offset a 前面是查询个数,后面是索引值
SELECT * FROM study LIMIT 0, 3; -- 查询第 0 到第 3 条记录(第一页,每页 3 条)
SELECT * FROM study LIMIT 3, 3; -- 查询第 3 到第 6 条记录(第二页,每页 3 条)select * from study limit 3,pagesize;--查询第 3 到最后一条记录
SELECT * FROM study LIMIT (page-1)*pagesize, pagesize; -- 分页查询通用公式
11.同时出现
顺序:先where(查找) 在order by 最后limit
select * from study where class = 1 order by age desc limit 0,2
12. 分组函数 聚合函数
sum()求和
max()最大值
min()最小值
avg()求平均
count()求记录数量 count(字段名)不统计为null的记录
select count(*) from student
select age from student
group by
select avg(age),class from student group by class having class = 1
计算 student 表中 class 为 1 的分组的平均年龄,并返回该平均年龄和对应的 class
select avg(age),class from student where sex="男" group by class having class = 1
先筛选出 student 表中性别为 "男" 的记录,然后计算这些记录中 class 为 1的分组的平均年龄,并返回该平均年龄和对应的 class(值为 1)
13.多表查询
select * from student,class where student.classid = class.id
14. 连表查询
join .. on
left join ...on
right join ...on多查询少查询
select * from student join class on student.classid = class.id
select * from student right join class on student.classid = class.id//student和class表,右连接 “包含右表全部数据 + 左右表交集数据”
15.重命名 as +名字 as可省
select * from student as s1,class c1 where s1.classid = c1.id
select student.*,class.name as classname from student,class where student.classid = class.id