数据库可自行创建。
就是基础主键id,姓名,性别,分数,课程,班级
{dotted startColor="#ff6c6c" endColor="#1989fa"/}
查询:
show databases; 查询库名
show tables; 查询表名
·Select from 表名1; 查询表名下的内容(*匹配所有字段)
----- 查询 表名course 下的所有字段
select * from course
·select 列名1,列名2 from 表名1; 查询表名1下列名1和列名2 的值
--------查询两个列表名下值
select c_id,cname from course

·Distinct 字段 去重查询
-------查询有几种类别
select distinct s_sex from student;
条件查询:
·Select * from 表名1 where 条件2; 使用where子句对表中数据筛选
--- 查询 表名student 下所有字段中s_id=1的值
select * from student where s_id=1;
---查询 表名student 下所有字段中 条件为s_id=1的s_name值
select s_name from student where s_id=1;
----查询 表名student 下所有字段中s_id>1的值
select * from student where s_id>1;
----查询 表名student 下所有字段中s_id<4的值
select * from student where s_id<4;
----查询 表名student 下所有字段中s_id!=4的值 (不等于 != <> )
select * from student where s_id !=4;
逻辑运算:
and 之间; or 以上; not 不在或不等于;
----查询 id在 2和6之间的所有学生信息
`select * from student where s_id<6 and s_id>2;`
----查询 id在 2和6之间的女学生信息
`select * from student where s_id<6 and s_sex='女';`
-----查询 id在4以上的女性 这个范围内的信息
select * from student where not s_id<4 and s_sex='女';
Ps:可以看出男的没出现,不符合题目意思
select * from student where not (s_id<4 and s_sex='女');
Ps;运算符优先级的问题 ,加括号就能解决
模糊查询:
--- like
--- % 替换任意个
--- _ 替换1个
--查询名字中 以“李”开始的名字
select * from student where s_name like '李%';
---查询名字中 有“雷”所有的名字
select * from student where s_name like '%雷%';
---查询名字中 有两个字的名字
select * from student where s_name like '__';
---查询名字中 有三个字的名字
select * from student where s_name like '___';
Ps:表中没有
---查询名字中 至少有两个字的名字
select * from student where s_name like '__%';
范围查询:
--- in (1,3, 8) 表示在一个非连续的范围内
--查询 id为2、5的姓名
select * from student where s_id in (2,5);
--- not in (1,3, 8) 表示在一个非连续的范围内
-- - between ... and .... 表示在一个连续的范围内
--查询id在2~5之间的信息
select * from student where s_id between 2 and 5;
-- not between ... and .... 表示不在一个连续的范围内
排序:
-- oeder by 字段
-- asc 从小到大排序 即升序
-- desc 从大到小 即降序
--s_id进行降序排列
select * from student order by s_id desc;
-- id 在 2和7之间的女性,id按照降序排列
select * from student where s_id between 2 and 7 and s_sex='女' order by s_id desc;
聚合函数:
总数:
Count()表示计算总行数,括号中写 与列名 ,结果是相同的。
--查询学生总数
select count(*) from student;
--查询男学生总数有多少
select count(*) from student where s_sex='男';
最大值:
Max(列)表示求此列的最大值
--查询女生的编号最大值
select max(s_id) from student where s_sex='女';

- 最小值:
Min(列)表示求此列的最小值
- 求和:
Sum()表示求和
- 平均值:
Avg()表示求平均值
--求id的平均值
select avg(s_id) from student;
---四舍五入 round(123.23 , 1) 保留1位小数
--计算所有人ID的平均值,保留2为小数
select round(avg(s_id),2) from student;
分组:
-- group by
---按照性别分组 ,查询所有的性别
select s_sex from student group by s_sex;
-- group_concat(name)
分组查询特定名字name
--查询同性别中的姓名
select s_sex,group_concat(s_name) from student group by s_sex;
--having
特定分组使用的 查询
-- having avg(age)>30 查询分组中年龄平均值大于30的
--查询性别分组中平均id大于1 的
select s_sex,avg(s_id) as names from student group by s_sex having avg(s_id) > 1;
--查询性别分组中人数大于3的
`select s_sex,count() from student group by s_sex having count() > 3;
`
-- with rollup
汇总的作用,(多一列合计)
--查询性别分组个人数和个人数之和
select s_sex,count(*) from student group by s_sex with rollup;
分页查询:
-- Limit
-- limit start,count limit 2,3 表示查询2页开始3个数据。
--查询前三行数据
select * from student limit 3;
--查询2开始5个数据
select * from student limit 2,5;
关联查询:
连接查询:
Inner join ... on
-- select .... from 表A inner join 表B on 相同项id;
-- select .... from 表A , 表B where 相同项id;
--查询学生的分数
select * from student as st inner join score as sc on st.s_id=sc.s_id;
--输出id和名字、分数
select student.s_id,student.s_name,score.s_score from student inner join score on student.s_id=score.s_id;
--查询 学生ID,名字,分数, 并以班级进行排序
select student.s_id,student.s_name,score.* from student inner join score on student.s_id=score.s_id order by score.c_id;
--当查询学生为同一班级, 以id进行从大到小排序
select student.s_id,student.s_name,score.* from student inner join score on student.s_id=score.s_id order by score.c_id,score.s_id;
左关联
left join ... on
-- select .... from 表A left join 表B on 相同项id;
右关联
Right join ... on
-- select .... from 表A right join 表B on 相同项id;
子查询:
-- 标量子查询(一个值)
--查询出高于平均id的信息
select avg(s_id) from student;
select * from student where s_id>4.5;
select * from student where s_id>(select avg(s_id) from student);
-- 行级子查询(一行值)
-- 列级子查询(一列值)
本文由 LceAn 创作,采用 知识共享署名4.0 国际许可协议进行许可。
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。