MySQL总和最后x个结果的平均值,其中结果可以在不同的列中

时间:2013-03-22 09:16:47

标签: mysql sql sum average

我有一个学校辩论数据库,我希望能够看到每所学校在过去5场辩论中的平均成绩。如果schoold正在举办辩论,那么得分记录在一个名为hostscore的专栏中,如果他们正在访问他们的分数就会进入visitcore。

这是我正在使用的数据的摘录

CREATE TABLE schools (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255)
) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB;

CREATE TABLE debates (
    debateid INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    debatedate DATE NOT NULL,
    hostid INT,
    visitid INT,
    hostscore INT,
    visitscore INT
) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB;

如果我这样做

SELECT debates.debateid, DATE_FORMAT(debates.debatedate,'%m-%d') AS DATE, school1.name AS HOST, school2.name AS VISITOR, debates.hostscore, debates.visitscore
FROM debates 
INNER JOIN schools as school1 ON debates.hostid=school1.id 
INNER JOIN schools as school2 ON debates.visitid=school2.id 
WHERE ((school1.id = 1 OR school2.id =1) ) AND debatedate < CURDATE()
ORDER BY debatedate DESC LIMIT 0 , 5

我可以看到他们最近的5次辩论。

如果我这样做。

SELECT visitid,
(
SELECT 
(
((SELECT sum(visitscore) FROM debates WHERE (visitid=1) AND debatedate < CURDATE()) + (SELECT sum(hostscore) FROM debates WHERE (hostid=1) AND debatedate < CURDATE()))
/
(SELECT COUNT(*) FROM debates WHERE ((visitid=1)or(hostid=1)) AND debatedate < CURDATE())
) 
FROM debates 
INNER JOIN schools as school1 ON debates.hostid=school1.id 
INNER JOIN schools as school2 ON debates.visitid=school2.id 
LIMIT 0,1
)
AS AVGSCORE 
FROM debates 
WHERE visitid=1 
LIMIT 0,1

我可以在今年的辩论中看到他们的平均分数。

但我无法弄清楚如何显示最后5场(整体)辩论(在下面的数据中,hostid = 1和visitid = 1的平均值)

    ----------------------------------------------------------------------
    |DEBATEID   |DATE   |HOST       |VISITOR    |HOSTSCORE  |VISITSCORE |
    |20         |09-22  |St Luke    |St Thomas  |82         |84         |
    |16         |08-22  |St Thomas  |St Simon   |91         |88         |
    |15         |08-12  |St Luke    |St Thomas  |75         |64         |
    |11         |07-12  |St Thomas  |St Simon   |72         |64         |
    |10         |06-28  |St Luke    |St Thomas  |82         |84         |
    ----------------------------------------------------------------------

从手动开始,这个例子中的结果应该是79.我在这里尝试过很多东西。我认为这看起来不错,但它产生的结果并不在我所寻找的附近。我究竟做错了什么?有关测试数据,请参阅此SQL Fiddle

SELECT sum(visitid),
(SELECT 
(
(
(SELECT sum(visitscore) FROM (SELECT visitscore FROM debates WHERE (hostid=1) AND debatedate < CURDATE() ORDER BY debatedate DESC LIMIT 0,5) as awayavgpg1)
+ 
(SELECT sum(visitscore) FROM (SELECT visitscore FROM debates WHERE (visitid=1) AND debatedate < CURDATE() ORDER BY debatedate DESC LIMIT 0,5) as awayavgpg2)
)
/
(SELECT COUNT(*) FROM (SELECT * FROM debates WHERE (hostid=1) or (visitid=1) AND debatedate < CURDATE()LIMIT 0,5) as awayavgpg3)
)
FROM debates 
LIMIT 0,1) AS AVSCORE
FROM debates 
WHERE visitid=1 
LIMIT 0,1

1 个答案:

答案 0 :(得分:2)

使用UNION将列合并为一个:

SELECT AVG(score) avscore
FROM (SELECT debatedate, score
      FROM (SELECT debatedate, hostscore score
            FROM debates
            WHERE hostid = 1
            UNION
            SELECT debatedate, visitscore score
            FROM debates
            WHERE visitid = 1) x
      ORDER BY debatedate DESC
      LIMIT 5) y