Java教程

二、数据查询语言(八)案例

本文主要是介绍二、数据查询语言(八)案例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

MySQL


一、初识数据库
二、数据查询语言(一)
二、数据查询语言(二)
二、数据查询语言(三)
二、数据查询语言(四)
二、数据查询语言(五)
二、数据查询语言(六)

二、数据查询语言(八)案例

  • MySQL
  • 前言
  • 一、建立student表
  • 二、在student表上操作
  • 总结

数据查询语言包括: 基础查询 条件查询 排序查询 分组查询 连接查询 子查询 分页查询 联合查询

前言


一、建立student表

我们用下面这段代码建立student表

DROP DATABASE IF EXISTS student;
CREATE DATABASE student;
USE student;

CREATE TABLE student(
	studentno VARCHAR(10) NOT NULL PRIMARY KEY,
	studentname VARCHAR(20) NOT NULL,
	loginpwd VARCHAR(8) NOT NULL,
	sex CHAR(1) ,
	majorid INT NOT NULL REFERENCES grade(majorid),
	phone VARCHAR(11),
	email VARCHAR(20) ,
	borndate DATETIME
);

CREATE TABLE major(
	majorid INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	majorname VARCHAR(20) NOT  NULL

);
CREATE TABLE result(
	id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
	studentno VARCHAR(10) NOT NULL REFERENCES student(studentno),
	score DOUBLE
);


INSERT INTO major VALUES(NULL,'javaee');
INSERT INTO major VALUES(NULL,'html5');
INSERT INTO major VALUES(NULL,'android');


INSERT INTO student VALUES('S001','张三封','8888','男',1,'13288886666','zhangsanfeng@126.com','1966-9-1');
INSERT INTO student VALUES('S002','殷天正','8888','男',1,'13888881234','yintianzheng@qq.com','1976-9-2');
INSERT INTO student VALUES('S003','周伯通','8888','男',2,'13288886666','zhoubotong@126.com','1986-9-3');
INSERT INTO student VALUES('S004','张翠山','8888','男',1,'13288886666',NULL,'1995-9-4');
INSERT INTO student VALUES('S005','小小张','8888','女',3,'13288885678','xiaozhang@126.com','1990-9-5');

INSERT INTO student VALUES('S006','张无忌','8888','男',2,'13288886666','zhangwuji@126.com','1998-8-9');
INSERT INTO student VALUES('S007','赵敏','0000','女',1,'13288880987','zhaomin@126.com','1998-6-9');
INSERT INTO student VALUES('S008','周芷若','6666','女',1,'13288883456','zhouzhiruo@126.com','1996-7-9');
INSERT INTO student VALUES('S009','殷素素','8888','女',1,'13288886666','yinsusu@163.com','1986-1-9');
INSERT INTO student VALUES('S010','宋远桥','6666','男',3,'1328888890','songyuanqiao@qq.com','1996-2-9');


INSERT INTO student VALUES('S011','杨不悔','6666','女',2,'13288882345',NULL,'1995-9-9');
INSERT INTO student VALUES('S012','杨逍','9999','男',1,'13288885432',NULL,'1976-9-9');
INSERT INTO student VALUES('S013','纪晓芙','9999','女',3,'13288888765',NULL,'1976-9-9');
INSERT INTO student VALUES('S014','谢逊','9999','男',1,'13288882211',NULL,'1946-9-9');
INSERT INTO student VALUES('S015','宋青书','9999','男',3,'13288889900',NULL,'1976-6-8');



INSERT INTO result VALUES(NULL,'s001',100);
INSERT INTO result VALUES(NULL,'s002',90);
INSERT INTO result VALUES(NULL,'s003',80);

INSERT INTO result VALUES(NULL,'s004',70);
INSERT INTO result VALUES(NULL,'s005',60);
INSERT INTO result VALUES(NULL,'s006',50);
INSERT INTO result VALUES(NULL,'s006',40);
INSERT INTO result VALUES(NULL,'s005',95);
INSERT INTO result VALUES(NULL,'s006',88);

使用SELECT * FROM student;可以查看此表,如图:
在这里插入图片描述

二、在student表上操作

一、查询每个专业的学生人数

SELECT majorid,COUNT(*)
FROM student
GROUP BY majorid;

二、查询参加考试的学生中,每个学生的平均分、最高分

#第一种做法是:
SELECT AVG(score),MAX(score),studentno
FROM result
GROUP BY studentno;
#第二种做法
SELECT AVG(score),MAX(score),studentname
FROM result r
INNER JOIN student s 
ON s.studentno=r.studentno
GROUP BY studentname;

三、查询姓张的每个学生的最低分大于60的学号、姓名

SELECT s.studentno,s.`studentname`,MIN(score)
FROM student s
INNER JOIN result r
ON s.`studentno`=r.`studentno`
WHERE s.`studentname` LIKE '张%'
GROUP BY s.`studentno`
HAVING MIN(score)>60;

四、查询生日在“1988-1-1”后的学生姓名、专业名称

SELECT s.`studentname`,`majorname`
FROM major m
INNER JOIN student s
ON m.majorid=s.majorid
WHERE DATEDIFF(borndate,'1988-1-1')>0;

五、查询每个专业的男生人数和女生人数分别是多少

#方式一
SELECT sex,COUNT(*) 个数,majorid
FROM student
GROUP BY sex,majorid
#方式二
SELECT majorid,
(SELECT COUNT(*) FROM student WHERE sex='男' AND majorid=s.majorid) 男,
(SELECT COUNT(*) FROM student WHERE sex='女' AND majorid=s.majorid) 女
FROM student s
GROUP BY majorid;

六、查询专业和张翠山一样的学生的最低分

#①查询张翠山的专业编号
SELECT studentname,majorid
FROM student
WHERE studentname='张翠山'

#查询和①中专业编号相同的学生
SELECT studentname
FROM student
WHERE majorid=(
	SELECT majorid
	FROM student
	WHERE studentname='张翠山')

#查询最低分
SELECT MIN(score)
FROM result r
INNER JOIN student s
ON r.studentno=s.studentno
WHERE studentname IN(
	SELECT studentname
	FROM student
	WHERE majorid=(
		SELECT majorid
		FROM student
		WHERE studentname='张翠山')

);

七、查询大于60分的学生的姓名、密码、专业名

SELECT studentname,loginpwd,majorname
FROM student s
INNER JOIN major m ON s.`majorid`=m.`majorid`
INNER JOIN result r ON r.`studentno`=s.`studentno`
WHERE score>60;

八、按邮箱位数分组,查询每组的学生个数

SELECT COUNT(*),LENGTH(email)
FROM student
GROUP BY LENGTH(email);	

九、查询学生名、专业名、分数

SELECT studentname,score,majorname
FROM student s
INNER JOIN major m ON s.`majorid`=m.`majorid`
INNER JOIN result r ON r.`studentno`=s.`studentno`;

十、查询哪个专业没有学生,分别用左连接和右连接实现

SELECT m.`majorid`,m.`majorname`,s.`studentno`
FROM major m
LEFT JOIN student s ON s.`majorid`=m.`majorid`
WHERE s.`studentno` IS NULL;

十一、查询没有成绩的学生人数

SELECT COUNT(*),r.id
FROM student s
LEFT JOIN result r ON r.`studentno`=s.`studentno`
WHERE score IS NULL

总结

期待大家和我交流,留言或者私信,一起学习,一起进步!

这篇关于二、数据查询语言(八)案例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!