消除了创建临时表的额外步骤

时间:2012-05-30 19:04:36

标签: mysql sql

我有一个查询,我用CREATE TEMPORARY TABLE AS SELECT执行,因为在我的生活中,我无法弄清楚如何将它写成一个查询,希望有人可以告诉我如何消除CREATE TEMPORARY TABLE AS

我的数据:

monitor table:
date | ip | webid | userid | compid

SQL

CREATE TEMPORARY TABLE temp AS SELECT webid, userid, COUNT(ip) AS total, COUNT(DISTINCT ip) AS uniq FROM monitor GROUP BY webid, userid;

SELECT SUM(total), SUM(uniq) FROM temp;

在将它们分组后,总和/ uniq的两个总和是我真正需要的,但如上所述,我想消除创建表格的步骤。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用派生表:

SELECT SUM(total), SUM(uniq) 
FROM (  SELECT webid, userid, COUNT(ip) AS total, COUNT(DISTINCT ip) AS uniq 
        FROM `monitor table`
        GROUP BY webid, userid) AS A

(我在临时表的定义中添加了FROM,因为它已经丢失了)