First acquainted with MySQL data query articles (below)

On the importance of MySQL

MySQL is a small relational database management system, developed by the Swedish MySQL software company, currently under the umbrella of Oracle. MySQL database not only has powerful functions but also has excellent compatibility and is easy for people to use. This paper mainly explores the MySQL in daily life Applications, important examples of the combination of MySQL database and cloud, and the important role of MySQL database in the Internet.

In the previous article, we introduced the basic query syntax knowledge of MySQL, from basic conditional queries to table join queries. These are the basic skills necessary for our daily development and data analysis. To be honest, the importance of MySQL is far more important than you master. Some knowledge of high-level programming syntax is important. Of course, if you are not engaged in data analysis and big data processing development, the application of MySQL may not be particularly extensive, but I still want to say that MySQL syntax knowledge is really important.

Insert picture description here
The importance of the database is very important in our daily data processing and development. The reason is that the database can store data sets efficiently and store a large amount of data in a specific mode. Then data analysts can use some MySQL query syntax to quickly Accurate and efficient structure to find out the data you need. Some people say whether such a non-professional person can also learn, I personally think it is still possible, although Excel can solve it, but when you need specific output some data how to display, MySQL makes your work can be one-click to solve you Little troubles.

The database is like the memory system of the human brain. Without the database, there would be no memory system. Computers will not develop so rapidly. The application of the database has penetrated into all aspects of life and work. The development of databases reflects the level of information development in a country, and many computer software developments are based on databases.

Not much to say, let's enter the MySQL query syntax exploration journey!

Download the data set here, click it, and then run it in your Navicat. All the following query statements can be used, including the series of syntax in the MySQL column. You can download it soon!

Review the previous query syntax

Specific query

SELECT * FROM `tb_teacher` 
WHERE gender='女' and title='副教授';

Query the type of teacher, use distinct to aggregate the same

SELECT DISTINCT TITLE FROM tb_teacher;
# COUNT 计算行数

SELECT COUNT(DISTINCT COURSE_CODE) as `课程种类数` from tb_course_class;

SELECT COUNT( DISTINCT COURSE_CLASS_CODE) as `班级种类数` from tb_electives;

Insert picture description here
Insert picture description here
Below I will use this method, I will directly post all the MySQL statements, you can run it yourself

-- 特定条件查询
SELECT * FROM `tb_teacher` 
WHERE gender='女' and title='副教授';

-- 聚合查询
SELECT DISTINCT TITLE FROM tb_teacher;

# COUNT 计算行数
SELECT COUNT(DISTINCT COURSE_CODE) as `课程种类数` from tb_course_class;
SELECT COUNT( DISTINCT COURSE_CLASS_CODE) as `班级种类数` from tb_electives;

# 全部学生人数
SELECT COUNT(*) from tb_student;

-- 查询班号为1的班级人数
SELECT count(*) from tb_student WHERE MAJOR_CLASS='1'

# 查询每个专业班的学生人数
SELECT COUNT(*) as sum,MAJOR_CLASS 
from tb_student 
GROUP BY MAJOR_CLASS 
ORDER BY MAJOR_CLASS;

# WHERE子句中使用子查询的情况,在子查询里面嵌套一些结果集,方便我们智能查询
SELECT * from tb_major_class where ID=(
SELECT MAJOR_CLASS from tb_student GROUP BY MAJOR_CLASS ORDER BY 
COUNT(*) DESC LIMIT 1);

# 查询课程班表中,每个老师带多少个班
-- 在进行分组查询的时候,你必须要清楚明白什么字段是重复的
那么相同的行就会整合到一组,然后根据该行进行一些查询
比如数量,平均分,其他属性
select COUNT(CODE) as `带班数`,teacher_id as `教师工号` from 
tb_course_class group by `教师工号` order by `带班数` DESC;

# 删除某字段包含NULL空值的行
DELETE FROM tb_course WHERE PERIOD IS NULL;

# 计算全部课程的学分之和
select SUM(CREDIT) as `学分之和`,PERIOD as `学分` from tb_course group by `学分`;

# 查询得分最高的分数和最低的分数,这里就充分利用到了分组的思想,然后根据分组里面的一些属性进行
一些查询
select course_class_code as 班号,MAX(SCORE) as 最高分,MIN(SCORE) as 最低分,
AVG(SCORE)as 平均分 from tb_electives group by course_class_code order by 平均分 DESC;

# 各学院专业各有多少班,这里采用了两个分组,为什么了,当我们需要获取一张大表里面的多个
重复字段的时候,我们需要分组这两个才可以
#这里的规则就是按照学院和入学年份进行分组,把同时具有这两个属性放在一组,
学院是2组,入学年份是4组,那么最后就分成了2*4=9组进行输出

-- 特别是在hive里面,进行多表连接的时候,要注意我们分组,如果是查询的字段在该表没有聚合分组里面,
那么就需要加入到分组里面

select college as `学院`,grade as `入学年份`,COUNT(*) as 班数 from tb_major_class 
group by `学院`, `入学年份`;

# 不同职称的老师各有多少名
select title as 职称,COUNT(*) as 人数 from tb_teacher group by title;

# 各班不及格的学生人数
select course_class_code 课程班编号,COUNT(*) 人数 from tb_electives where score<60
group by course_class_code;

# 不及格人数超过10人的班级,GROUP BY having
select course_class_code 课程班编号,COUNT(*) 人数 from tb_electives where score<60
group by course_class_code having 人数>10;

select * from tb_electives having score <50;

# 错误!聚合字段是在groupby之后产生,不能再where中使用
select course_class_code ,COUNT(*) 人数 from tb_electives where score<60 and 人数>10
group by course_class_code;

# 统计每个老师上几门课程
select COUNT(DISTINCT COURSE_CODE) 课程数,teacher_id 老师编号 from 
tb_course_class group by teacher_id ;

# order by 要放到语句的最后面
select COUNT(DISTINCT COURSE_CODE) 课程数,teacher_id from tb_course_class
 GROUP BY teacher_id having 课程数>4 order by 课程数;

Here is a summary of some basic queries, which has rich reference significance

--查询自己班级编号
SELECT t.id AS 班级编号 
FROM
	tb_major_class AS t 
WHERE
	t.major = '大数据' 
	AND t.GRADE = '2019' 
	AND t.CLASS = '3';
--插入个人信息到学生选课表,利用子查询 

INSERT INTO tb_student SELECT
'2019443888',
'王小王',
'男',
'2020-12-25',
'15730596588',
ID 
FROM
	tb_major_class 
WHERE
	major = '大数据' 
	AND grade = 2019 
	AND class = 3;
SELECT
	* 
FROM
	tb_student 
WHERE
	`NAME` = '王小王';
--查看与自己同名或者同姓的学生 
SELECT
* 
FROM
	tb_student 
WHERE
	NAME = '%小王' 
	OR NAME LIKE '王%';
--统计你所在班级的人数 
SELECT
count(*) AS 班级人数 
FROM
	tb_student 
WHERE
	major_class =(
	SELECT
		ID 
	FROM
		tb_major_class 
	WHERE
		major = '大数据' 
		AND grade = 2019 
		AND class = 3 
	);
--统计你所在班级男生的人数
SELECT
count(*) 男生人数 
FROM
	tb_student AS t 
WHERE
	t.GENDER = '男' 
	AND major_class =(
	SELECT
		ID 
	FROM
		tb_major_class 
	WHERE
		major = '大数据' 
		AND grade = 2019 
		AND class = 3 
	);
--统计你所在班级女生的人数 SELECT
count(*) 女生人数 
FROM
	tb_student AS t 
WHERE
	t.GENDER = '女' 
	AND major_class =(
	SELECT
		ID 
	FROM
		tb_major_class 
	WHERE
		major = '大数据' 
		AND grade = 2019 
		AND class = 3 
	);
--列出你所在的学院的专业名称 

SELECT DISTINCT
t.major 专业名称 
FROM
	tb_major_class AS t 
WHERE
	college = '数理与大数据学院';
	
-- --统计各课程班级的最高分和最低分

SELECT
	a.`NAME` 课程名称,
	b.COURSE_CODE 课程 ID,
	c.* 
FROM
	tb_course AS a,
	tb_course_class AS b,
	( SELECT COURSE_CLASS_CODE 课程班级 ID, max( SCORE ) 最高分, 
	min( SCORE ) 最低分 FROM tb_electives GROUP BY COURSE_CLASS_CODE ) AS c 
WHERE
	a.`CODE` = b.COURSE_CODE 
	AND b.`CODE` = c.`课程班级ID`;
	
-- 	统计平均分超过75分的课程班级及平均分,并按平均分从小到大排序
SELECT
	* 
FROM
	(
	SELECT
		b.COURSE_CODE 课程 ID,
		a.`NAME` 课程名称,
		c.`平均分` 
	FROM
		tb_course AS a,
		tb_course_class AS b,
		( SELECT COURSE_CLASS_CODE 课程班级 ID, AVG( SCORE ) 平均分 
		FROM tb_electives GROUP BY
		COURSE_CLASS_CODE HAVING AVG( SCORE )> '75' ) AS c 
	WHERE
		a.`CODE` = b.COURSE_CODE 
		AND b.`CODE` = c.`课程班级ID` 
	) AS e 
ORDER BY
	e.`平均分`;
--查询上’结构化数据存储与处理’课程的老师的姓名 
SELECT DISTINCT
t.老师名称 
FROM
	(
	SELECT
		a.NAME 课程名称,
		c.NAME 老师名称 
	FROM
		tb_course AS a,
		tb_course_class AS b,
		tb_teacher AS c 
	WHERE
		a.CODE = b.course_code 
		AND b.TEACHER_ID = c.id 
	) AS t 
WHERE
	t.课程名称 = '结构化数据存储与处理';
SELECT DISTINCT
	TEACHER_ID 
FROM
	tb_course_class 
WHERE
	COURSE_CODE = '3SL1037A'

One word per text

When you are bored, look at the outside world. Wonderful far can mobilize your emotions and motivation.

Guess you like

Origin blog.csdn.net/weixin_47723732/article/details/111667120