Mysql第三章(高级查询)

/*
检查“Logic Java”课程最近一次考试成绩
如果有80分以上的成绩,显示分数排在前5名的学员学号和分数
*/
SELECT studentNo,studentresult
FROM result
WHERE EXISTS( 
	SELECT * FROM result
	WHERE examDate=(
	SELECT MAX(examDate)
	FROM result 
	WHERE subjectNo=(
	SELECT subjectNo
	FROM `subject`
	WHERE subjectName='Logic Java'
	) )
	
AND studentResult>80
)
AND subjectNo=(
	SELECT subjectNo
	FROM `subject`
	WHERE subjectName='Logic Java')
AND examDate=(
	SELECT MAX(examDate)
	FROM result 
	WHERE subjectNo=(
	SELECT subjectNo
	FROM `subject`
	WHERE subjectName='Logic Java'))
ORDER BY studentResult DESC
LIMIT 5


/*
检查“Logic Java”课程最近一次考试成绩
如果全部未通过考试(60分及格),认为本次考试偏难,计算的该次考试平均分加5分
*/

SELECT AVG(studentResult+5)
FROM result
WHERE subjectNo=(
	SELECT subjectNo
	FROM `subject`
	WHERE subjectName='Logic Java')
AND examDate=(
	SELECT MAX(examDate)
	FROM result 
	WHERE subjectNo=(
	SELECT subjectNo
	FROM `subject`
	WHERE subjectName='Logic Java'))
AND NOT EXISTS(
	SELECT * FROM result
	WHERE  subjectNo=(
	SELECT subjectNo
	FROM `subject`
	WHERE subjectName='Logic Java')
AND examDate=(
	SELECT MAX(examDate)
	FROM result 
	WHERE subjectNo=(
	SELECT subjectNo
	FROM `subject`
	WHERE subjectName='Logic Java'))
	AND studentResult>100
	)

常用的多表链接查询

# 查询学生的考试成绩 显示学生姓名,科目ID 成绩
#内联

SELECT studentName,subjectNo,studentResult
FROM student AS s
INNER JOIN result AS r ON s.studentNo=r.studentNo

#直联
SELECT studentName,subjectNo,studentResult
FROM student AS s, result AS r
WHERE s.studentNo=r.studentNo

左外链接(left join)#以左表为主表 主表没有在从表做过的以NULL 补充

右外链接(right join)

 

/*
为每个学生制作在校期间每门课程的成绩单,
要求每个学生参加每门课程的最后一次考试作为该生本课程的结业成绩
成绩单的数据项 学生姓名 课程所属的年级名称 课程名称 考试日期 考试成绩
*/
SELECT studentName,sb.subjectNo,subjectName,examDate,studentResult
FROM student AS s
INNER JOIN  result AS r ON s.studentNo=r.studentNo
INNER JOIN `subject` AS sb ON sb.subjectNo=r.subjectNo
WHERE examDate=(
	SELECT MAX(examDate) FROM result
	WHERE studentNo=s.studentNo AND subjectNo=sb.subjectNo
	)

房屋出租数据

create database house; USE `house`
CREATE TABLE `hos_district` (
  `DID` int(4) NOT NULL AUTO_INCREMENT,
  `dName` varchar(4) NOT NULL,
  PRIMARY KEY (`DID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

insert  into `hos_district`(`DID`,`dName`) values (1,'海淀'),(2,'朝阳'),(3,'丰台');

CREATE TABLE `hos_house` (
  `hMID` int(4) NOT NULL AUTO_INCREMENT,
  `UID` int(4) NOT NULL,
  `SID` int(4) NOT NULL,
  `hTID` int(4) NOT NULL,
  `price` decimal(10,2) DEFAULT '0.00',
  `topic` varchar(50) NOT NULL,
  `contents` varchar(255) NOT NULL,
  `hTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `copy` varchar(500) DEFAULT NULL,
  PRIMARY KEY (`hMID`),
  KEY `fk_house_district` (`UID`),
  KEY `fk_house_street` (`SID`),
  KEY `fk_house_type` (`hTID`),
  CONSTRAINT `fk_house_district` FOREIGN KEY (`UID`) REFERENCES `sys_user` (`UID`),
  CONSTRAINT `fk_house_street` FOREIGN KEY (`SID`) REFERENCES `hos_street` (`SID`),
  CONSTRAINT `fk_house_type` FOREIGN KEY (`hTID`) REFERENCES `hos_type` (`hTID`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

insert  into `hos_house`(`hMID`,`UID`,`SID`,`hTID`,`price`,`topic`,`contents`,`hTime`,`copy`) values (1,1,1,1,'800.00','万泉庄','万泉庄一号','2016-12-14 17:03:55',NULL),(2,1,1,2,'800.00','万泉庄','万泉庄二号','2016-04-14 17:03:55',NULL),(3,1,2,1,'800.00','苏州街','苏州街一号','2016-01-14 17:03:55',NULL),(4,1,2,3,'800.00','苏州街','苏州街二号','2016-09-14 17:03:55',NULL),(5,1,3,1,'800.00','中关村','中关村一号','2016-10-14 17:03:55',NULL),(6,1,3,3,'800.00','中关村','中关村二号','2016-03-14 17:03:55',NULL),(7,1,4,2,'800.00','东四','东四二号','2016-09-14 17:03:55',NULL),(8,1,4,1,'800.00','东四','东四一号','2016-11-14 17:03:55',NULL),(9,1,4,3,'800.00','东四','东四三号','2016-09-14 17:03:55',NULL);

CREATE TABLE `hos_street` (
  `SID` int(4) NOT NULL AUTO_INCREMENT,
  `sName` varchar(50) NOT NULL,
  `sDID` int(4) NOT NULL,
  PRIMARY KEY (`SID`),
  KEY `fk_street_district` (`sDID`),
  CONSTRAINT `fk_street_district` FOREIGN KEY (`sDID`) REFERENCES `hos_district` (`DID`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

insert  into `hos_street`(`SID`,`sName`,`sDID`) values (1,'万泉庄',1),(2,'苏州街',1),(3,'中关村',1),(4,'东四',2),(5,'三里屯',2),(6,'望京',2);

CREATE TABLE `hos_type` (
  `hTID` int(4) NOT NULL AUTO_INCREMENT,
  `hTName` varchar(10) NOT NULL,
  PRIMARY KEY (`hTID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

insert  into `hos_type`(`hTID`,`hTName`) values (1,'一室一厅'),(2,'两室一厅'),(3,'三室一厅');


CREATE TABLE `sys_user` (
  `UID` int(4) NOT NULL AUTO_INCREMENT,
  `uName` varchar(10) NOT NULL,
  `uPassWord` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`UID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;


insert  into `sys_user`(`UID`,`uName`,`uPassWord`) values (1,'张三','11111');
/*hos_house 
 hmid 出租屋编号 uid 客户编号
 sid 街道编号 hTid 房屋类型编号 price 每月租金 topic 标题 contents 描述

 house _district 
 DID 区县编号 dName  区县名称`hos_district``hos_district``hos_street`

`hos_street`
SID 街道编号 sName 街道名称 sDId 区县编号

`hos_type`
hTID 户型编号 hTName 户型类型

`sys_user`
UID 编号 uName 户主姓名 uPassWord 租金

*/
#查询输出第6条~第10条出租房屋信息
SELECT * FROM `hos_house`
LIMIT 5,5
#查询张三发布的所有出租房屋信息,并显示房屋分布的街道和区县
SELECT * FROM `hos_house`
WHERE UID=(SELECT UID FROM `sys_user` WHERE uName='张三')
/*
使用LIMIT关键字实现查询数据分页显示
使用临时表保存临时的查询结果
需求说明
查询输出第6条~第10条出租房屋信息
*/
CREATE TABLE house1(
	SELECT * FROM `hos_house`
	LIMIT 5,5
	)
DROP TABLE IF EXISTS house1
#查询张三发布的所有出租房屋信息,并显示房屋分布的街道和区县

SELECT *
FROM `hos_street` AS s
INNER JOIN `hos_district`AS d ON d.DID=s.sDID
INNER JOIN `hos_house` AS h ON h.SID=s.SID
INNER JOIN `sys_user` AS u ON u.UID=h.UID
WHERE uName='张三'
#子查询
SELECT * FROM `hos_house` AS h
INNER JOIN `hos_street` AS s ON s.SID=h.SID
INNER JOIN `hos_district` AS d ON d.DID=s.sDID
WHERE UID=(SELECT UID FROM `sys_user` WHERE uName='张三')

#根据户型和房屋所在区县和街道,为至少有两个街道有出租房屋的区县制作出租房屋清单
SELECT HTname,dName,sNAme,uName
FROM `hos_street` AS s
INNER JOIN `hos_district`AS d ON d.DID=s.sDID
INNER JOIN `hos_house` AS h ON h.SID=s.SID
INNER JOIN `sys_user` AS u ON u.UID=h.UID
INNER JOIN `hos_type` AS t ON t.HtID=h.HTID
WHERE d.`DID` IN (
	SELECT sdid FROM `hos_street`
	WHERE SID IN(
		SELECT SID
		FROM `hos_house`
		GROUP BY `SID`
	)
	GROUP BY sdid
	HAVING COUNT(*)>1
)
/*按季度统计出本年各区县各街道各种户型房屋出租数量
输出2016年从1月1日起至今的
全部出租房屋数量、各区县出租房屋数量及各街道、户型出租房屋数量
SELECT QUARTER(NOW())
*/

	SELECT QUARTER(`hTime`) AS 季度,' 合计'AS 区县,'' AS 街道,'' AS 户型,COUNT(*) AS 出租数量
	FROM `hos_house`
	GROUP BY 季度
UNION
	SELECT QUARTER(`hTime`) AS 季度,dname AS 区县,' 小计' AS 街道,'' AS 户型,COUNT(*) AS 出租数量
	FROM `hos_house` AS h
	INNER JOIN hos_street AS s ON s.sid =h.sid
	INNER JOIN hos_district AS d ON  d.did=s.sdid
	GROUP BY 季度,d.did
UNION 
	SELECT QUARTER(`hTime`) AS 季度,dname AS 区县,sname AS 街道,htname AS 户型,COUNT(*) AS 出租数量
	FROM `hos_house` AS h
	INNER JOIN hos_street AS s ON s.sid =h.sid
	INNER JOIN hos_district AS d ON  d.did=s.sdid
	INNER JOIN hos_type AS t ON t.htid=h.htid
	GROUP BY 季度,d.did,s.sid,t.htid
ORDER BY 1,2,3,4







你可能感兴趣的:(Mysql第三章(高级查询))