如何取出 sql 中的百分比

How to take out percentage in sql

我有 sql 名学生出勤 table,我在其中输入了如下值:

Student_id total_classes class_attended
1          31            26 
2          31            21
3          31            17
4          31            21
5          31            29

我想用1计算student_id的出勤率。 我正在寻找如何为上述内容构建 sql 语句..

只需要将出席人数类除以总数再乘以100即可:

SELECT student_id, ROUND(class_attended/total_classes*100) total FROM student

以上将return以下结果:

+------------+-------+
| student_id | total |
+------------+-------+
|          1 |    84 |
|          2 |    68 |
|          3 |    55 |
|          4 |    68 |
|          5 |    94 |
+------------+-------+

您可以使用 select 查询,例如

SELECT class_attended/total_classes AS 出席率 从出席 其中 Student_id = '1'