Php 如何在一行九分之间选择七个高分并求和

Php 如何在一行九分之间选择七个高分并求和,php,mysql,Php,Mysql,大家好!我试图从网站上找到之前问题的解决方案,但我发现这些问题都与我无关,我需要帮助,了解如何在表中同一行的九个分数中选择七个分数,并将其相加,如下所示 name | math | geography | history | pds | ict | civics | social | vskil | french Joseph 100 90 80 84 70 40 70 90 70 所以这里应该

大家好!我试图从网站上找到之前问题的解决方案,但我发现这些问题都与我无关,我需要帮助,了解如何在表中同一行的九个分数中选择七个分数,并将其相加,如下所示

name   | math | geography | history | pds | ict | civics | social | vskil |  french

Joseph   100    90          80        84    70    40       70       90        70
所以这里应该选择以下七(7)个分数(100+90+80+84+70+90+70)


请提供任何帮助。

您需要将列转换为行,然后进行选择

您可以尝试此查询

select sum(grade) from (
(select math as 'grade' from table1 where name = 'Joseph')
union all
(select geography from table1 where name = 'Joseph')
union all
(select history from table1 where name = 'Joseph')
union all
(select pds from table1 where name = 'Joseph')
union all
(select ict from table1 where name = 'Joseph')
union all
(select civics from table1 where name = 'Joseph')
union all
(select social from table1 where name = 'Joseph')
union all
(select vskil from table1 where name = 'Joseph')
union all
(select french from table1 where name = 'Joseph')
) as x
order by grade desc
limit 7

您需要将列转换为行,然后进行选择

您可以尝试此查询

select sum(grade) from (
(select math as 'grade' from table1 where name = 'Joseph')
union all
(select geography from table1 where name = 'Joseph')
union all
(select history from table1 where name = 'Joseph')
union all
(select pds from table1 where name = 'Joseph')
union all
(select ict from table1 where name = 'Joseph')
union all
(select civics from table1 where name = 'Joseph')
union all
(select social from table1 where name = 'Joseph')
union all
(select vskil from table1 where name = 'Joseph')
union all
(select french from table1 where name = 'Joseph')
) as x
order by grade desc
limit 7

使用下表结构(而不是当前表结构)可以获得更好的性能,使用更少的存储空间,并使代码更易于编写

表学生:

id integer primary key
name varchar
表主题

studentid integer primary key references students(id)
subject varchar
marks int
真正的优化将意味着一个主题表和一个主题分数表。请详细阅读数据库规范化

对于给定学生的这种结构

SELECT SUM(marks) 
FROM (SELECT marks FROM subjects WHERE studentid=some_number ORDER BY marks DESC limit 7) AS a

给出给定学生的前7个科目的分数

使用下表结构而不是当前表结构,可以获得更好的性能、使用更少的存储空间并使代码更易于编写

表学生:

id integer primary key
name varchar
表主题

studentid integer primary key references students(id)
subject varchar
marks int
真正的优化将意味着一个主题表和一个主题分数表。请详细阅读数据库规范化

对于给定学生的这种结构

SELECT SUM(marks) 
FROM (SELECT marks FROM subjects WHERE studentid=some_number ORDER BY marks DESC limit 7) AS a

给出给定学生的前7个科目的分数

您的表结构不正确。主题,标记应该是行而不是列,如果它假设是结构化的。请给我举个例子,你的表格结构不正确。主题,标记应该是行而不是列,如果它假设是结构化的。请给我举个例子谢谢你,我应该尝试使用这种方法,因为直到现在问题仍然存在,我尝试了下面的查询,但它只是将所有9个结果相加,而不是7个。太好了。让我知道它是如何进行的。我必须对你们说声谢谢,下面是“开发人员”查询运行,并在更改结构后提供所需的答案。多谢各位。这个网站非常有帮助,非常欢迎你。我很高兴能帮上忙。既然你还是新来的,我想指出的是,在这里说“谢谢”的首选方式是投票选出好的问题和有用的答案(一旦你有足够的声誉这么做),并接受对你提出的任何问题最有用的答案(这也会给你的声誉带来一点提升)。谢谢你,我应该尝试使用这种方法,因为直到现在问题仍然存在,我尝试了下面的查询,但它只是将所有9个结果相加,而不是7个。太好了。让我知道它是如何进行的。我必须对你们说声谢谢,下面是“开发人员”查询运行,并在更改结构后提供所需的答案。多谢各位。这个网站非常有帮助,非常欢迎你。我很高兴能帮上忙。既然你还是新来的,我想指出的是,在这里说“谢谢”的首选方式是投票选出好的问题和有用的答案(一旦你有足够的声誉这么做),并接受对你提出的任何问题最有用的答案(这也会给你的声誉带来一点提升)。