select max() from count()

11 浏览
0 Comments

select max() from count()

我需要找到每个用户的最大行动数\n表列:action_id、action_status、user\n请求:\nSELECT MAX(`counted`) FROM\n(\nSELECT COUNT(*) AS `counted`\nFROM `table_actions`\nWHERE `status` = \"good\"\nGROUP BY `user`\n)\n错误信息:“每个派生表必须有自己的别名”\n出了什么问题?..

0
0 Comments

问题原因:问题的原因是在使用嵌套查询时,MySQL要求给内部的SELECT语句一个名称。

解决方法:要解决这个问题,只需要给内部的SELECT语句一个名称即可,如下所示:

SELECT MAX(counted) FROM
(
    SELECT COUNT(*) AS counted
    FROM table_actions
    WHERE status = "good"
    GROUP BY user
) AS counts;

此外,如果在查询中还选择了status字段,结果将不准确。以下查询语句将无法工作,因为字段"status"将不准确:

SELECT MAX(counted), status FROM
(
    SELECT COUNT(*) AS counted, status
    FROM table_actions
    WHERE status = "good"
    GROUP BY user
) AS counts;

对于这个问题的解决方法,目前我不清楚如何修复。

0