mysql - SQL:如何对别名计算列 ADD 添加计数进行排序?

标签 mysql sql

我是 SQL 新手,很难从数据库中获取信息,也许有人可以提供指导。我有

表格:学生

列:

name, notes, test_1_score, test_2_score, test_3_score, test_4_score, test_5_score,
test_6_score, test_7_score, test_8_score, test_9_score, test_10_score

我可以让下面的代码在没有GROUP BY中的points_achieved的情况下运行,但这就是我想要排序的。另外,如果不添加 IsNull,我就无法进行计算。

SELECT
name, 
notes, 
SUM (IsNull (test_1_score, 0) + IsNull (test_2_score, 0) + IsNull (test_3_score, 0) + IsNull (test_4_score, 0) + IsNull (test_5_score, 0) +IsNull ( test_6_score, 0) + IsNull (test_7_score, 0) + IsNull (test_8_score, 0) + IsNull (test_9_score, 0) + IsNull (test_10_score, 0)) AS points_acheived
FROM students
GROUP BY
points_achieved, name, notes;

最终,我想要一个简单的答案: 名称points_achieved(添加已完成的#个测试的计数)…….notes

非常感谢任何帮助,谢谢。

最佳答案

试试这个:

SELECT
name, 
notes, 
SUM (IsNull (test_1_score, 0) + IsNull (test_2_score, 0) + IsNull (test_3_score, 0) + IsNull (test_4_score, 0) + IsNull (test_5_score, 0) +IsNull ( test_6_score, 0) + IsNull (test_7_score, 0) + IsNull (test_8_score, 0) + IsNull (test_9_score, 0) + IsNull (test_10_score, 0)) AS points_acheived
FROM students
GROUP BY
points_achieved, name, notes
order by 3;

关于mysql - SQL:如何对别名计算列 ADD 添加计数进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26299943/

相关文章:

MySQL:高效地返回组不为空的分组字段

php - SQL PHP 检索特定商店的总金额

mysql - 如何将查询直接传递给where条件?

SQL Server 加入不相关的表

mysql - ORDER BY ASC 底部有空值

mysql - 如何在Postgresql中选择一列显示为具有差异值的2列结果

Mysql 函数 TO_SECONDS 不存在

php - 如何在同一个字幕中显示所有图像?

sql - 在 Web 应用程序中提供快速 `select count(*)` 功能

mysql - SQL 删除时间戳早于(现在 - x 天)的所有行,但保留最近的 n 条记录