我有两个或多或少相同的表:
test_score
id int(11) PK AI
user_id int(11)
module_id int(11)
score double
number_correct int(11)
correct_total int(11)
timestamp datetime
phase_id int(11)
medal_id int(11)
team_id int(11)
status_id int(11) 和
id int(11) PK AI
user_id int(11)
module_id int(11)
score double
number_correct int(11)
correct_total int(11)
timestamp datetime
phase_id int(11)
medal_id int(11)
team_id int(11)
status_id int(11) 现在,这两者之所以如此相同,是因为它们包含来自我的系统中各自组件的数据。
现在我想使用AVG()函数来找出这两个表的总和的平均分数。
这个是可能的吗?
发布于 2014-10-09 20:20:46
SELECT user_id, average_score = AVG(score)
FROM (
SELECT user_id, score FROM test_score1
UNION ALL
SELECT user_id, score FROM test_score2
) AS subquery
GROUP BY user_id发布于 2014-10-09 20:22:07
不要只考虑表,而要考虑“结果集”。
将问题分解为两个部分。
首先,如何从这两个表中创建我想要的数据的“结果集”?最简单的解决方案是union语句
select * from <table1>
union
select * from <table2>现在,获取上面的结果集并计算平均值
select xx.module_id,avg(xx.score) as AvgModuleScore
from
(
select * from <table1>
union
select * from <table2>
) xxhttps://stackoverflow.com/questions/26278147
复制相似问题