• 数据库第三章 参考


    1、

    #查询S2学员考试成绩信息
    SELECT `studentNo` AS 学号,`subjectNo` AS 科目编号,`studentResult` AS 成绩,`examDate` AS 考试时间 FROM `result` 
    WHERE EXISTS(
    SELECT * FROM `student` WHERE `gradeId` = (
        SELECT `gradeId` FROM `grade` WHERE `gradeName`='S2')
        ) AND `subjectNo` = (
        SELECT `subjectNo` FROM `subject` WHERE `gradeId` = (
        SELECT `gradeId` FROM `grade` WHERE `gradeName`='S2' ));

    2、

    SELECT  `studentName` 姓名,  
       ( SELECT `gradeName` FROM `grade` WHERE `gradeId`=`subject`.`gradeId`  ) AS 课程所属年级 , 
    `subjectName` 课程名称, `examDate` 考试日期, `studentResult` 成绩
    FROM  `result` r1
      INNER JOIN `student` ON r1.`studentNo`=`student`.`studentNo`
      INNER JOIN `subject` ON `subject`.`subjectNo`=r1.`subjectNo`
    WHERE r1.`examDate` IN (
          SELECT MAX(`examDate`) FROM `result` r2
          WHERE r1.`subjectNo` = r2.`subjectNo`#主查询和子查询间参数值绑定
          GROUP BY r2.`subjectNo`
    ) 
    ORDER BY subject.gradeId;

    3、

    /*--------------统计考试缺考情况--------------*/
    
    SELECT ( SELECT COUNT(*)  FROM `student`                
                     WHERE `gradeId`=(SELECT `gradeId` FROM `subject` WHERE `subjectName`= 'Logic Java')
                     ) AS 应到人数 ,
       
          ( SELECT COUNT(*) FROM `result` 
                    WHERE `examDate`=(SELECT MAX(`examDate`) FROM `result` INNER JOIN  `subject`
    ON `result`.`subjectNo`=`subject`.`subjectNo` WHERE `subjectName`='Logic Java') 
                    AND `subjectNo` = (SELECT `subjectNo` FROM `SUBJECT` WHERE `subjectName`= 'Logic Java')
                   ) AS 实到人数,
         (
                    SELECT COUNT(*)  FROM `student`                
                     WHERE `gradeId`=(SELECT `gradeId` FROM `subject` WHERE `subjectName`= 'Logic Java')
                  ) -
                  (
                    SELECT COUNT(*) FROM `result` 
                    WHERE `examDate`=(SELECT MAX(`examDate`) FROM `result` INNER JOIN  `subject`
    ON `result`.`subjectNo`=`subject`.`subjectNo` WHERE `subjectName`='Logic Java') 
                    AND `subjectNo` = (SELECT `subjectNo` FROM `subject` WHERE `subjectName`= 'Logic Java')
                  ) AS 缺勤人数; 
    
    /*---------统计考试通过情况,并将统计结果存放在新表TempResult中---------*/
    DROP TABLE IF EXISTS `tempResult`;
    CREATE TABLE `tempResult`(
    SELECT  `student`.`studentName`,`student`.`studentNo`,`studentResult`
    FROM `student` LEFT JOIN (
                           SELECT * FROM `result` 
                    WHERE `examDate`=(SELECT MAX(`examDate`) FROM `result` INNER JOIN  `subject`
    ON `result`.`subjectNo`=`subject`.`subjectNo` WHERE `subjectName`='Logic Java') 
                    AND `subjectNo` = (SELECT `subjectNo` FROM `subject` WHERE `subjectName`= 'Logic Java')
                            ) R
    ON `student`.`studentNo`=R.`studentNo` )
          
    #SELECT * FROM TempResult --查看统计结果,可用于调试

    4、

    #查询租房信息第6~10条记录
    SELECT * FROM hos_house ORDER BY hMID LIMIT 5,5;
    
    #使用临时表保存临时的查询结果
    DROP TABLE IF EXISTS tmp_table;
    CREATE TEMPORARY TABLE tmp_table(
      SELECT * FROM hos_house ORDER BY hMID LIMIT 5,5
    );
    #创建数据库
    DROP DATABASE IF EXISTS house;
    CREATE DATABASE house;
    
    USE house;
    #创建客户信息表
    DROP TABLE IF EXISTS `sys_user`;
    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 DEFAULT CHARSET=utf8;
    
    #创建区县信息表
    DROP TABLE IF EXISTS `hos_district`;
    CREATE TABLE `hos_district` (
      `DID` INT(4) NOT NULL AUTO_INCREMENT,
      `dName` VARCHAR(4) NOT NULL,
      PRIMARY KEY (`DID`)
    ) ENGINE=INNODB DEFAULT CHARSET=utf8;
    
    #创建街道信息表
    DROP TABLE IF EXISTS `hos_street`;
    
    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 DEFAULT CHARSET=utf8;
    
    
    
    #创建房屋类型表
    DROP TABLE IF EXISTS `hos_type`;
    CREATE TABLE `hos_type` (
      `hTID` INT(4) NOT NULL AUTO_INCREMENT,
      `hTName` VARCHAR(10) NOT NULL,
      PRIMARY KEY (`hTID`)
    ) ENGINE=INNODB DEFAULT CHARSET=utf8;
    
    #创建出租房屋信息表
    DROP TABLE IF EXISTS `hos_house`;
    
    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 DEFAULT CHARSET=utf8;

    5、

    #方法一:使用联接查询
    SELECT hos_district.DNAME AS '区县',hos_street.SNAME AS '街道',HTID AS '户型',
    PRICE AS '价格',TOPIC AS '标题',CONTENTS AS '描述',HTIME AS '时间',COPY AS '备注'
    FROM hos_house INNER JOIN sys_user ON (hos_house.UID=sys_user.UID)
                      INNER JOIN hos_street ON(hos_house.SID=hos_street.SID)
                      INNER JOIN hos_district ON(hos_Street.SDID=hos_district.DID)
    WHERE sys_user.UNAME='张三';
    
    
    #方法二:使用子查询
    SELECT hos_district.DNAME AS '区县',hos_street.SNAME AS '街道',HTID AS '户型',
    PRICE AS '价格',TOPIC AS '标题',CONTENTS AS '描述',HTIME AS '时间',COPY AS '备注'
    FROM hos_house INNER JOIN sys_user ON (hos_house.UID=sys_user.UID)
                      INNER JOIN hos_street ON(hos_house.SID=hos_street.SID)
                      INNER JOIN hos_district ON(hos_Street.SDID=hos_district.DID)
    WHERE hos_house.UID = (SELECT UID FROM sys_user WHERE UName = '张三');

    6、

    SELECT hos_type.HTNAME AS '户型',sys_user.UNAME AS '姓名',
    
    hos_district.DNAME AS '区县',hos_street.SNAME AS '街道'
    
    FROM hos_house INNER JOIN sys_user ON (hos_house.UID=sys_user.UID)
    
                      INNER JOIN hos_street ON(hos_house.SID=hos_street.SID)
    
                      INNER JOIN hos_district ON(hos_street.SDID=hos_district.DID)
    
                      INNER JOIN hos_type ON(hos_house.HTID=hos_type.HTID)
    
    WHERE hos_street.SDID IN (
    
          SELECT hos_street.SDID
      FROM hos_house INNER JOIN hos_street ON
                     (hos_house.SID=hos_street.SID)
    
                     INNER JOIN hos_district ON(hos_street.SDID=hos_district.DID)
    
          GROUP BY hos_street.SDID
      HAVING COUNT(hos_street.SDID)>1
    
       );
        

    7、

    SELECT tmp.quarter AS '季度',hos_district.DNAME AS '区县',hos_street.SNAME AS '街道',
    hos_type.HTNAME AS '户型',tmp.cnt AS '房屋数量'
    FROM (
      SELECT SID,HTID,COUNT(*) cnt,QUARTER(HTIME) `quarter`
      FROM hos_house
      WHERE YEAR(HTIME)=2016
      GROUP BY QUARTER(HTIME),SID,HTID
    ) tmp
    INNER JOIN hos_street ON(tmp.SID=hos_street.SID)
    INNER JOIN hos_district ON(hos_street.SDID=hos_district.DID)
    INNER JOIN hos_type ON(tmp.HTID=hos_type.HTID)
    UNION
    SELECT QUARTER(HTIME),hos_district.DNAME,' 小计 ','  ',COUNT(*) AS '房屋数量'
    FROM hos_house
    INNER JOIN hos_street ON(hos_house.SID=hos_street.SID)
    INNER JOIN hos_district ON(hos_street.SDID=hos_district.DID)
    WHERE YEAR(HTIME)=2016
    GROUP BY QUARTER(HTIME),hos_district.DNAME
    UNION
    SELECT QUARTER(HTIME),' 合计 ','  ','  ',COUNT(*) AS '房屋数量'
    FROM hos_house
    WHERE YEAR(HTIME)=2016
    GROUP BY QUARTER(HTIME)
    ORDER BY 1,2,3,4;
  • 相关阅读:
    页面跳转
    基于MCP2515的Linux CAN总线驱动程序设计(三)
    基于MCP2515的Linux CAN总线驱动程序设计(二)
    基于MCP2515的Linux CAN总线驱动程序设计(一)
    任意ASCII码格式信息的huffman tree压缩(编码)和解压(译码)
    转:Linux环境下段错误的产生原因及调试方法小结
    转:C语言中volatile关键字的作用 专家博客
    处理字节对齐
    转: sizeof,总结
    sizeof()用法汇总
  • 原文地址:https://www.cnblogs.com/yunfeioliver/p/9415894.html
Copyright © 2020-2023  润新知