html - 使用Mysql时如何创建多个组的 View

标签 html mysql

我正在开展一个项目,需要一份报告。我想依靠这份报告 2014 年 1 月,哪个类(class)的学生进入了 1 月份。学生表只有类(class)号。并且表的表包含了所有的信息。

       Group No 1. month and year.
Group No 2. Class
My STD Table

+--------+----------+-----------------+----------------+------------+ | STD_ID | STD_name | Admission_Month | Admission_year | Class_Code | +--------+----------+-----------------+----------------+------------+ | 01 | ABC | January | 2017 | 1 | +--------+----------+-----------------+----------------+------------+ | 02 | DEF | January | 2017 | 2 | +--------+----------+-----------------+----------------+------------+ |03 | GHI | February | 2017 | 1 | +--------+----------+-----------------+----------------+------------+

我需要这个输出

+--------+----------+-----------------+----------------+
| Month  | Year     | Class           | Count_Student  |
+--------+----------+-----------------+----------------+
|January | 2017     |     01          |    01          |
+--------+----------+-----------------+----------------+
|January | 2017     |     02          |    01          |
+--------+----------+-----------------+----------------+
|February| 2014     |     01          |    01          | 
+--------+----------+-----------------+----------------+

请大家帮帮我

最佳答案

您可以尝试此解决方案来解决您的问题:

表结构:

CREATE TABLE student ( 
  STD_ID MEDIUMINT NOT NULL AUTO_INCREMENT,
  STd_name varchar(25),
  Admission_Month varchar(25),
  Admission_year varchar(25),
  Class_Code varchar(25),
  std_Status varchar(25),
  PRIMARY KEY (STD_ID) ) #ENGINE=MyISAM;

CREATE TABLE CLASS ( 
  Class_Code MEDIUMINT NOT NULL AUTO_INCREMENT,
  Class_name varchar(25),
  CLass_Courcess varchar(25),
PRIMARY KEY (Class_Code) ) 
#ENGINE=MyISAM;


INSERT INTO student (STd_name, Admission_Month, Admission_year, Class_Code,std_Status)
VALUES ("ABC","January","2017","1","On"),
       ("DEF","January","2017","1","On"),
       ("GHI","February","2017","2","Off"),
       ("ADFDBC","MARCH","2017","1","On");


INSERT INTO CLASS (Class_name,CLass_Courcess)
VALUES("DIV A","MBA"),("DIV B","MTECH")

查询:

    SELECT Admission_Month as Month, Admission_year as Year, Class_name as Class, 
    (SELECT COUNT(s_on.STD_ID) FROM student as s_on WHERE s_on.Admission_Month = student.Admission_Month AND s_on.Admission_year = student.Admission_year AND s_on.Class_Code = student.Class_Code AND s_on.std_Status ="On") as status_on, 
    (SELECT COUNT(s_on.STD_ID) FROM student as s_on WHERE s_on.Admission_Month = student.Admission_Month AND s_on.Admission_year = student.Admission_year AND s_on.Class_Code = student.Class_Code AND s_on.std_Status ="Off") as status_off
    FROM student
    LEFT JOIN class ON class.Class_Code = student.Class_Code
    GROUP BY Admission_Month,Admission_year

您也可以引用sqlfiddle

希望对您有所帮助。

关于html - 使用Mysql时如何创建多个组的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47902819/

相关文章:

javascript - 数据表子行处理空值

html - CSS 自动改变 ul 宽度;适合 child inline-block li 的最小宽度?

mysql - laravel - 将查询结果从 HomeController 传递到主 Blade View

mysql - 添加一列,默认值为后天日期

mysql - 如何在Windows后台运行apache的httpd守护进程?

html - 尝试将 css 链接添加到文件但失败,因为路径中有空格

javascript - 使用 ngStyle 将样式分配给 tr :first-child td

javascript - jQuery 网站标题问题

mysql - SQL如何对给定范围的列进行分组并连接到其他表以计算与第一列相关的行

mysql - MySQL 中数据的请求格式