【问题标题】:Get results for multiple rows in MySQL table using view使用视图获取 MySQL 表中多行的结果
【发布时间】:2021-07-14 04:27:44
【问题描述】:

我还是一个学习 MySQL 的学生。我有一个数据库名称“tech_lms”和一个包含 136 行的表“出席”。

使用此表,我想获取“absent_present”列中“present”的学生的百分比。所以我为此创建了一个 SQL 视图。

    CREATE VIEW eligible AS
    SELECT studentID,subID, 
      (COUNT(absent_present 
       WHERE absent_present="present")/COUNT(absent_present))*100 AS presentage 
FROM attendence;

但我无法获得预期的结果。我的代码中可能有一些错误。如果有人可以帮助我,我将不胜感激。

【问题讨论】:

    标签: mysql view


    【解决方案1】:

    您的 sql 有语法错误,请尝试以下操作:

    CREATE VIEW eligible AS
    SELECT studentID,subID, 
      SUM(IF(absent_present='present',1,0))/COUNT(absent_present)*100 AS presentage 
     FROM attendence group by studentID;
    

    【讨论】:

    • 非常感谢!这可以节省我的时间。这段代码有一个变化。那就是最后一个“)”必须被删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 2012-01-16
    • 2021-03-15
    • 2015-09-09
    相关资源
    最近更新 更多