简体   繁体   English

从临时表返回MySQL单行

[英]MySQL Single Row Returned From Temporary Table

I am running the following queries against a database: 我正在对数据库运行以下查询:

CREATE TEMPORARY TABLE med_error_third_party_tmp
SELECT `med_error_category`.description AS category, `med_error_third_party_category`.error_count AS error_count
FROM
    `med_error_category` INNER JOIN `med_error_third_party_category` ON med_error_category.`id` = `med_error_third_party_category`.`category`
WHERE
    year = 2003
GROUP BY `med_error_category`.id;

The only problem is that when I create the temporary table and do a select * on it then it returns multiple rows, but the query above only returns one row. 唯一的问题是,当我创建临时表并对其进行选择*时,它将返回多行,但是上面的查询仅返回一行。 It seems to always return a single row unless I specify a GROUP BY, but then it returns a percentage of 1.0 like it should with a GROUP BY. 除非我指定了GROUP BY,否则它似乎总是返回单行,但随后它会像使用GROUP BY一样返回1.0的百分比。

SELECT category, 
       error_count/SUM(error_count) AS percentage
  FROM med_error_third_party_tmp;

Here are the server specs: 以下是服务器规格:
Server version: 5.0.77 伺服器版本:5.0.77
Protocol version: 10 协议版本:10
Server: Localhost via UNIX socket 服务器:通过UNIX套接字的Localhost

Does anybody see a problem with this that is causing the problem? 有人看到引起问题的问题吗?

Standard SQL requires you to specify a GROUP BY clause if any column is not wrapped in an aggregate function (IE: MIN, MAX, COUNT, SUM, AVG, etc), but MySQL supports "hidden columns in the GROUP BY" -- which is why: 如果未在聚合函数(IE:MIN,MAX,COUNT,SUM,AVG等)中包装任何列,则标准SQL要求您指定GROUP BY子句,但MySQL支持“ GROUP BY中的隐藏列”也就是为什么:

SELECT category, 
       error_count/SUM(error_count) AS percentage
  FROM med_error_third_party_tmp;

...runs without error. ...运行无误。 The problem with the functionality is that because there's no GROUP BY, the SUM is the SUM of the error_count column for the entire table. 该功能的问题在于,因为没有GROUP BY,所以SUM是整个表的error_count列的SUM。 But the other column values are completely arbitrary - they can't be relied upon. 但是其他列的值完全是任意的-无法依靠它们。

This: 这个:

SELECT category, 
       error_count/(SELECT SUM(error_count)
                      FROM med_error_third_party_tmp) AS percentage
  FROM med_error_third_party_tmp;

...will give you a percentage on a per row basis -- category values will be duplicated because there's no grouping. ...将为您提供每行一个百分比-类别值将重复,因为没有分组。

This: 这个:

  SELECT category, 
         SUM(error_count)/x.total AS percentage
    FROM med_error_third_party_tmp
    JOIN (SELECT SUM(error_count) AS total
            FROM med_error_third_party_tmp) x 
GROUP BY category

...will gives you a percentage per category of the sum of the categories error_count values vs the sum of the error_count values for the entire table. ...将为您提供整个类别表的error_count值总和与error_count值总和的每个类别的百分比。

another way to do it - without the temp table as seperate item... 另一种方法-不用临时表作为单独的项目...

select category, error_count/sum(error_count) "Percentage"
from (SELECT mec.description category
          ,  metpc.error_count 
      FROM med_error_category mec
         , med_error_third_party_category metpc
      WHERE mec.id = metpc.category
      AND year = 2003 
      GROUP BY mec.id
     );

i think you will notice that the percentage is unchanging over the categories. 我认为您会注意到类别中的百分比没有变化。 This is probably not what you want - you probably want to group the errors by category as well. 这可能不是您想要的-您可能还希望按类别对错误进行分组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM