1

I have a query that I execute with CREATE TEMPORARY TABLE AS SELECT because for the life of me I can't figure out how to write it as one query, hoping someone can show me how to eliminate the CREATE TEMPORARY TABLE AS.

My data:

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;

The two sums for total/uniq after it's been grouped, are all I really need but as mentioned I'd like to eliminate the step of creating the table.

thanks in advance for any help.

4

1 回答 1

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在您对临时表的定义中添加了一个,因为它丢失了)

于 2012-05-30T19:06:32.617 回答