0

嗨,我有类似的数据库

   name    to   Maths Science
    stud1 stud2   30    50
  **stud1 stud1   40    60**
    stud2 stud2   20    90
  **stud3 stud1   60    80**

当我将我的数据库查询为 stud1 时,我需要获取输出,因为我需要为他自己获取 stud1 的值,他评价和所有值的平均值属于他在上面有 **

 AVg(Maths) Maths
    50           40

任何人都可以建议我如何进行此查询。

4

3 回答 3

0
SELECT Name, (SELECT AVG(maths) from tbl),MATHS from tbl
于 2013-09-26T18:41:31.170 回答
0

Avg() 函数是一个组操作,而 select 是一个行操作。您不应该尝试在单个查询中将它们组合在一起,因为它包含冗余信息。但是,如果您想继续,请执行以下操作:

select to, name, maths, science, (select avg(maths) from table), 
(select avg(science) from table) from table where to ="stud1";
于 2013-09-26T18:47:03.697 回答
0

嗯,不知道为什么答案倾向于忽略平均值:

SELECT
  AVG(Maths) AS AvgMaths,
  MAX(IF(name=to), Maths, NULL)) AS Maths
FROM some_table WHERE to = 'stud1'

注意:我原以为平均值是:43。(3 行中有“stud1”,而不是 2)

于 2013-09-26T19:02:10.327 回答