day02rk/day02rk.sql

134 lines
3.8 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
Navicat Premium Data Transfer
Source Server : 106.54.193.225
Source Server Type : MySQL
Source Server Version : 50736
Source Host : 106.54.193.225:3306
Source Schema : day02rk
Target Server Type : MySQL
Target Server Version : 50736
File Encoding : 65001
Date: 19/07/2024 09:07:19
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_course
-- ----------------------------
DROP TABLE IF EXISTS `t_course`;
CREATE TABLE `t_course` (
`course_id` int(11) NOT NULL AUTO_INCREMENT,
`course_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`course_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of t_course
-- ----------------------------
INSERT INTO `t_course` VALUES (1, '语文');
INSERT INTO `t_course` VALUES (2, '数学');
INSERT INTO `t_course` VALUES (3, '英语');
-- ----------------------------
-- Table structure for t_scope
-- ----------------------------
DROP TABLE IF EXISTS `t_scope`;
CREATE TABLE `t_scope` (
`scope_id` int(11) NOT NULL AUTO_INCREMENT,
`course_id` int(11) NULL DEFAULT NULL,
`student_id` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`scope_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of t_scope
-- ----------------------------
INSERT INTO `t_scope` VALUES (1, 1, 1);
INSERT INTO `t_scope` VALUES (2, 1, 2);
INSERT INTO `t_scope` VALUES (3, 1, 3);
INSERT INTO `t_scope` VALUES (4, 2, 3);
INSERT INTO `t_scope` VALUES (5, 2, 4);
INSERT INTO `t_scope` VALUES (6, 2, 5);
INSERT INTO `t_scope` VALUES (7, 3, 6);
INSERT INTO `t_scope` VALUES (8, 3, 7);
INSERT INTO `t_scope` VALUES (9, 3, 8);
INSERT INTO `t_scope` VALUES (10, 3, 9);
-- ----------------------------
-- Table structure for t_student
-- ----------------------------
DROP TABLE IF EXISTS `t_student`;
CREATE TABLE `t_student` (
`student_id` int(11) NOT NULL AUTO_INCREMENT,
`student_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`student_sex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`student_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 99 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of t_student
-- ----------------------------
INSERT INTO `t_student` VALUES (1, '张三', '');
INSERT INTO `t_student` VALUES (2, 'CVBS', '');
INSERT INTO `t_student` VALUES (3, '理论', '');
INSERT INTO `t_student` VALUES (4, '解决', '');
INSERT INTO `t_student` VALUES (5, '慢慢', '');
INSERT INTO `t_student` VALUES (6, '利旧', '');
INSERT INTO `t_student` VALUES (7, '赵六', '');
INSERT INTO `t_student` VALUES (8, '田七', '');
INSERT INTO `t_student` VALUES (9, '李四', '');
INSERT INTO `t_student` VALUES (10, '王五', '');
SET FOREIGN_KEY_CHECKS = 1;
SELECT count(student_sex),student_sex count FROM t_student GROUP BY student_sex
-- 3.查询男生和女生各多少人。15分
SELECT count(student_sex) count,student_sex FROM t_student GROUP BY student_sex
-- 4. 查询男生和女生分别占总人数的比例。20分
SELECT count(student_id) count FROM t_student WHERE COUNT(student_sex) GROUP BY student_sex
-- 5.查询每门课程下的学生总人数。15分
SELECT
count(*),c.course_name
FROM
t_scope s
LEFT JOIN t_course c ON s.course_id = c.course_id
LEFT JOIN t_student t ON t.student_id = s.student_id
GROUP BY c.course_id