MySQL快速入门
in MySQL with 0 comment
MySQL快速入门
in MySQL with 0 comment

数据库可自行创建。
就是基础主键id,姓名,性别,分数,课程,班级

{dotted startColor="#ff6c6c" endColor="#1989fa"/}

查询:
show databases; 查询库名
show tables; 查询表名
1

·Select from 表名1; 查询表名下的内容(*匹配所有字段)

----- 查询 表名course 下的所有字段

select * from course

2

·select 列名1,列名2 from 表名1; 查询表名1下列名1和列名2 的值

--------查询两个列表名下值

select c_id,cname from course

3

·Distinct 字段 去重查询
Test

-------查询有几种类别

select distinct s_sex from student;
Test

条件查询:

·Select * from 表名1 where 条件2; 使用where子句对表中数据筛选

--- 查询 表名student 下所有字段中s_id=1的值

select * from student where s_id=1;
Test

---查询 表名student 下所有字段中 条件为s_id=1的s_name值

select s_name from student where s_id=1;

Test

----查询 表名student 下所有字段中s_id>1的值

select * from student where s_id>1;

Test

----查询 表名student 下所有字段中s_id<4的值

select * from student where s_id<4;
Test

----查询 表名student 下所有字段中s_id!=4的值 (不等于 != <> )

select * from student where s_id !=4;

Test

Test

逻辑运算:

and 之间; or 以上; not 不在或不等于;

----查询 id在 2和6之间的所有学生信息

`select * from student where s_id<6 and s_id>2;` 

Test

----查询 id在 2和6之间的女学生信息

`select * from student where s_id<6 and s_sex='女';` 

Test

-----查询 id在4以上的女性 这个范围内的信息

select * from student where not s_id<4 and s_sex='女';
Test
Ps:可以看出男的没出现,不符合题目意思

select * from student where not (s_id<4 and s_sex='女');
Test
Ps;运算符优先级的问题 ,加括号就能解决

模糊查询:

--- like

--- % 替换任意个

--- _ 替换1个

--查询名字中 以“李”开始的名字

select * from student where s_name like '李%';
Test

---查询名字中 有“雷”所有的名字
select * from student where s_name like '%雷%';

Test

---查询名字中 有两个字的名字

select * from student where s_name like '__';
Test

---查询名字中 有三个字的名字

select * from student where s_name like '___';

Test

Ps:表中没有

---查询名字中 至少有两个字的名字

select * from student where s_name like '__%';

Test

范围查询:

--- in (1,3, 8) 表示在一个非连续的范围内

--查询 id为2、5的姓名

select * from student where s_id in (2,5);

Test

--- not in (1,3, 8) 表示在一个非连续的范围内

-- - between ... and .... 表示在一个连续的范围内

--查询id在2~5之间的信息

select * from student where s_id between 2 and 5;

Test

-- not between ... and .... 表示不在一个连续的范围内

排序:

-- oeder by 字段

-- asc 从小到大排序 即升序

-- desc 从大到小 即降序

--s_id进行降序排列

select * from student order by s_id desc;

Test

-- id 在 2和7之间的女性,id按照降序排列

select * from student where s_id between 2 and 7 and s_sex='女' order by s_id desc;
Test

聚合函数:

总数:

Count()表示计算总行数,括号中写 与列名 ,结果是相同的。

--查询学生总数

select count(*) from student;

Test

--查询男学生总数有多少

select count(*) from student where s_sex='男';
Test

最大值:

Max(列)表示求此列的最大值

--查询女生的编号最大值

select max(s_id) from student where s_sex='女';

Test

Min(列)表示求此列的最小值

Sum()表示求和

Avg()表示求平均值

--求id的平均值

select avg(s_id) from student;

Test

---四舍五入 round(123.23 , 1) 保留1位小数

--计算所有人ID的平均值,保留2为小数

select round(avg(s_id),2) from student;

Test

分组:

-- group by

---按照性别分组 ,查询所有的性别

select s_sex from student group by s_sex;

Test

-- group_concat(name)

分组查询特定名字name

--查询同性别中的姓名

select s_sex,group_concat(s_name) from student group by s_sex;

Test

--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;

Test

--查询性别分组中人数大于3的

`select s_sex,count() from student group by s_sex having count() > 3;
`
Test

-- with rollup

汇总的作用,(多一列合计)

--查询性别分组个人数和个人数之和

select s_sex,count(*) from student group by s_sex with rollup;
Test

分页查询:

-- Limit

-- limit start,count limit 2,3 表示查询2页开始3个数据。

--查询前三行数据

select * from student limit 3;

Test

--查询2开始5个数据

select * from student limit 2,5;

Test

关联查询:

连接查询:

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;

Test

--输出id和名字、分数

select student.s_id,student.s_name,score.s_score from student inner join score on student.s_id=score.s_id;

Test

--查询 学生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;
Test

--当查询学生为同一班级, 以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;
Test

左关联

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;

Test

select * from student where s_id>4.5;
Test

select * from student where s_id>(select avg(s_id) from student);
Test

-- 行级子查询(一行值)

-- 列级子查询(一列值)

Responses