0

即使在阅读了所有相关主题之后,也没有任何帮助:

$q="select count(*) from (
        SELECT COUNT(*) AS total
          FROM clips
         WHERE ((dimensions like('%concert%') AND dimensions NOT like('concert'))
            OR (file_format like('%concert%') AND file_format NOT like('concert')))
         UNION DISTINCT 
        SELECT COUNT(*) AS total
          FROM clips
         WHERE (dimensions like('concert') OR file_format like('concert'))
    ) AS num";
$q2 = mysql_query($q) or die(mysql_error());
$q3 = mysql_fetch_array($q2);
echo $q3['num'];

我得到的错误:

注意:未定义的索引:num in .... on line ..

我究竟做错了什么?

4

5 回答 5

2

您正在设置num表别名,而不是计数。试试这个

$q="select count(*) AS num from (
    SELECT COUNT(*) AS total
      FROM clips
     WHERE ((dimensions like('%concert%') AND dimensions NOT like('concert'))
        OR (file_format like('%concert%') AND file_format NOT like('concert')))
     UNION DISTINCT
    SELECT COUNT(*) AS total
      FROM clips
     WHERE (dimensions like('concert') OR file_format like('concert'))
) AS sub_query_alias_that_is_required_but_pointless";
$q2 = mysql_query($q) or die(mysql_error());
$q3 = mysql_fetch_array($q2);
echo $q3['num'];

但是,您可能正在寻找的更多预期功能是计算总行数,而不是像您拥有的那样计算计数。此外,由于您只是查询clips表,我认为您根本不需要 UNION。根据您在问题中的逻辑,我认为(但如果没有您的实际数据就无法对其进行测试)它在逻辑上等同于这个非常简单的查询,除非我严重遗漏了一些东西。这个查询可能更友好、更简洁。

SELECT COUNT(1) AS num
FROM clips
WHERE dimensions like '%concert%' 
   OR file_format like '%concert%'
于 2012-06-26T15:22:35.790 回答
1

您的查询计算名为“num”的子查询的结果。

这意味着没有名为 num 的结果,您可以select count(*) as myNum from...为此执行类似操作

现在您要计算的是:

(SELECT COUNT(*) AS total 
 FROM clips
 WHERE ((dimensions like('%concert%') 
         AND dimensions NOT like('concert')) 
       OR (file_format like('%concert%') AND file_format NOT like('concert'))) 
 UNION DISTINCT 
 SELECT COUNT(*) AS total FROM clips 
  WHERE (dimensions like('concert') OR file_format like('concert'))) AS num";

这会给你一个计数,而不是结果。所以你是在计算数量,而不是把它们加在一起。

于 2012-06-26T15:24:11.703 回答
1

一旦你重新格式化你的查询,你会看到最后一个“as num”是指整个选择,而不是 count(*),我猜这是你想要的别名。

尝试始终为每个条目在单独的行中格式化您的查询,这对您的调试有很大帮助。

于 2012-06-26T15:24:48.833 回答
1

我编辑了您的问题以更好地布置查询。问题是您将别名分配给子选择而不是结果值。此外,当您计算行数时,出于性能原因,这样做会更好count(1)count(*)因为count(1) simply uses the constant in counting, whilecount(*)` 必须在增加计数之前检索实际行。试试这个:

$q="select count(1) as num from (
        SELECT COUNT(1) AS total
          FROM clips
         WHERE ((dimensions like('%concert%') AND dimensions NOT like('concert'))
            OR (file_format like('%concert%') AND file_format NOT like('concert')))
         UNION DISTINCT
        SELECT COUNT(1) AS total
          FROM clips
         WHERE (dimensions like('concert') OR file_format like('concert'))
    ) AS sub";
于 2012-06-26T15:25:39.127 回答
0

您的查询(一旦为最外层命名count(*) as num,将返回 1 或 2,具体取决于两个子查询中别名为“total”的表达式是否相等。

不必使用 UNION 并扫描clips表两次以返回此结果。一次扫描就足够了。这个查询将返回一个等效的结果,对剪辑表进行一次扫描:

SELECT CASE WHEN c.total_1 = c.total_2 THEN 1 ELSE 2 END AS num
  FROM
(
SELECT SUM(CASE WHEN ((dimensions like('%concert%') AND dimensions NOT like('concert'))
                  OR (file_format like('%concert%') AND file_format NOT like('concert')))
                THEN 1 ELSE 0 END
          ) AS total_1
     , SUM(CASE WHEN (dimensions like('concert') OR file_format like('concert'))
                THEN 1 ELSE 0 END
          ) AS total_2
  FROM clips
) c

这可能会比您的查询更有效地运行。

尽管如此,这是否是您真正追求的结果集尚不清楚。


附录:更正。这个查询并不完全等价。当剪辑表为空(不包含行)时,此查询将返回不同的结果。这可以修复,但我不相信这真的是 Mark 所追求的结果集。

要处理剪辑表为空的情况:

SELECT CASE WHEN IFNULL(c.total_1,0) = IFNULL(c.total_2,0) THEN 1 ELSE 2 END AS num
  FROM (SELECT 1) i LEFT JOIN
(
SELECT SUM(CASE WHEN ((dimensions like('%concert%') AND dimensions NOT like('concert'))
                  OR (file_format like('%concert%') AND file_format NOT like('concert')))
                THEN 1 ELSE 0 END
          ) AS total_1
     , SUM(CASE WHEN (dimensions like('concert') OR file_format like('concert'))
                THEN 1 ELSE 0 END
          ) AS total_2
  FROM clips
) c
于 2012-06-26T15:39:39.697 回答