/*
 Navicat Premium Data Transfer

 Source Server         : lyx
 Source Server Type    : MySQL
 Source Server Version : 50736
 Source Host           : 111.229.181.183:3306
 Source Schema         : test002

 Target Server Type    : MySQL
 Target Server Version : 50736
 File Encoding         : 65001

 Date: 19/07/2024 09:07:53
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for stu_cou
-- ----------------------------
DROP TABLE IF EXISTS `stu_cou`;
CREATE TABLE `stu_cou`  (
  `sc_id` int(11) NOT NULL AUTO_INCREMENT,
  `c_id` int(11) NULL DEFAULT NULL,
  `s_id` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`sc_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 14 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of stu_cou
-- ----------------------------
INSERT INTO `stu_cou` VALUES (1, 1, 1);
INSERT INTO `stu_cou` VALUES (2, 1, 2);
INSERT INTO `stu_cou` VALUES (3, 1, 5);
INSERT INTO `stu_cou` VALUES (4, 2, 3);
INSERT INTO `stu_cou` VALUES (5, 2, 4);
INSERT INTO `stu_cou` VALUES (6, 2, 7);
INSERT INTO `stu_cou` VALUES (7, 2, 9);
INSERT INTO `stu_cou` VALUES (8, 3, 10);
INSERT INTO `stu_cou` VALUES (9, 3, 6);
INSERT INTO `stu_cou` VALUES (10, 3, 8);
INSERT INTO `stu_cou` VALUES (11, 2, 8);
INSERT INTO `stu_cou` VALUES (12, 1, 10);
INSERT INTO `stu_cou` VALUES (13, 3, 2);

-- ----------------------------
-- Table structure for t_course
-- ----------------------------
DROP TABLE IF EXISTS `t_course`;
CREATE TABLE `t_course`  (
  `c_id` int(11) NOT NULL AUTO_INCREMENT,
  `c_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`c_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 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_student
-- ----------------------------
DROP TABLE IF EXISTS `t_student`;
CREATE TABLE `t_student`  (
  `s_id` int(11) NOT NULL AUTO_INCREMENT,
  `s_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `sex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`s_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 21 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, '李四', '女');
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(*) AS count
FROM t_student
WHERE sex = '男';

-- 查询女生人数
SELECT COUNT(*) AS n_count
FROM t_student
WHERE sex = '女';

-- 查询男生占比
SELECT COUNT(*) * 100.0 / (SELECT COUNT(*) FROM t_student) AS people_nan
FROM t_student
WHERE sex = '男';

-- 查询女生占比
SELECT COUNT(*) * 100.0 / (SELECT COUNT(*) FROM t_student) AS people_nv
FROM t_student
WHERE sex = '女';

-- 查询每门课程下的学生总人数
SELECT c.c_name AS course_name, COUNT(sc.s_id) AS student_count
FROM t_course c
       LEFT JOIN stu_cou sc ON c.c_id = sc.c_id
GROUP BY c.c_name
