查找
select 查找
* 要找的字段
from + 要查的表
eg: select * from student;
查询出来的结果是一个二维表
条件查询
关键字 where
eg: select * from student where class_num='20201001';
查询男性的名字
select name from student where sex='男';
同时查询男性的名字和ID
select name,id from student where sex='男';
多条件
select from student where sex='男' or/and age='20'
找出不是男性的
select * from student where not sex='男'
去重查询
select DISTINCT sex from student
一个就只出现一次
模糊查询
select * from student where name like '王%'
会查出所有的王某某
select * from student where name like '%王'
出查出所有的某某王
select * from student where name like '%王%'
查出所有的 “某某王某某”
select * from student where name like '王_'
只会查出来 王某(王加一个字)
查询排序
按年龄排序
select * from student order by age
倒叙排序 DESC
按年龄大的在上面排序
select * from student order by age DESC
限制查询
只显示前三条
select * from student LIMIT 0,3
从0开始查询,查询3个
select * from student LIMIT 3 OFFSET 0
# 限制查询 --- 分页
# limit 限制查询的关键字
# 我们要查询出前 3 条数据
# limit 后边根的是我们限制查询的条数
# OFFSET 后边根的是从第几条数据开始查询 默认从 0 开始
# (pageIndex - 1) * pageSize
# SELECT * from student LIMIT pageSize OFFSET (pageIndex - 1) * pageSize
#聚合查询
#查询出student 表当中一共有对少条记录
# count() 查询数据库当中有多少条记录
SELECT count(*) from student
# sum() 计算某一列当中的合计值 这一列必须为数值类型
SELECT suM(age) from student
# AVG() 计算某一列当中的平均值 这一列必须为数值类型
SELECT avg(age) from student
分组查询 group by
select count(*) from student GROUP BY class_num
数据库语句记录
最新推荐文章于 2024-08-14 11:26:43 发布