我有一个 MySQL 表,其中记录了回答问题的用户以及试验次数。就像是:
username trials
-------- ------
user1 10
user2 7
user1 20
等等。从中我可以计算出他们回答问题的次数(COUNT
)。现在我想计算平均试验次数,即得到下表:
username avg
-------- ---
user1 15
user2 7
我试过这个查询:
SELECT(a.totalguess/b.totalknow) as avg FROM( SELECT username, SUM(trials) AS totalguess FROM thetable GROUP BY username) a, (SELECT username, COUNT(*) as totalknow FROM thetable GROUP BY username)b WHERE a.username=b.username;
它只给了
avg
---
15
7
八九不离十!但没有知情者。我可能可以将它们与 php 结合起来,但我想要一个纯 MySQL 解决方案。所以我该怎么做?提前致谢!