MySQL:如何计算复杂查询返回的行数

时间:2011-08-22 14:06:50

标签: php mysql count

这感觉它应该很容易,但我无法得到它而且我已经尝试了所有东西。

我如何获取此查询返回的行数:

(SELECT Alias1 . *, 
        Alias2 . * 
 FROM   Table1 AS Alias1 
        LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
 WHERE  Alias1.Col1 != 'blah1' 
        AND Alias1.Col2 LIKE 'blah2' 
        AND ( Alias1.Col3 LIKE 'blah3' 
               OR Alias1.Col3 LIKE 'blah4' ) 
        AND Alias1.Col4 = 'blah5' 
        AND Alias2.Col7 LIKE 'blah6' 
 ORDER  BY Alias1.Col6 DESC 
 LIMIT  50) 

UNION 

(SELECT Alias1 . *, 
        Alias2 . * 
 FROM   Table1 AS Alias1 
        LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
 WHERE  Alias1.Col1 != 'blah1' 
        AND Alias1.Col2 LIKE 'blah2' 
        AND ( Alias1.Col3 LIKE 'blah3' 
               OR Alias1.Col3 LIKE 'blah4' ) 
        AND Alias1.Col4 = 'blah5' 
        AND Alias2.Col5 LIKE 'blah6' 
 ORDER  BY Alias1.Col6 DESC 
 LIMIT  50) 

UNION 

(SELECT Alias1 . *, 
        Alias2 . * 
 FROM   Table1 AS Alias1 
        LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
 WHERE  Alias1.Col1 != 'blah1' 
        AND Alias1.Col2 LIKE 'blah2'
        AND ( Alias1.Col3 LIKE 'blah3' 
               OR Alias1.Col3 LIKE 'blah4' ) 
        AND Alias1.Col4 = 'blah5' 
 ORDER  BY Alias1.Col6 DESC 
 LIMIT  50) 

我想我可以使用PHP执行查询,然后对结果执行mysql_num_rows,但我想直接使用SQL,因为我听说它节省了一步更快。

谢谢!

编辑:

以下是给我错误(#1060 - 重复列名'Col1'):

   SELECT COUNT(*) FROM (
   (SELECT Alias1 . *, 
            Alias2 . * 
     FROM   Table1 AS Alias1 
            LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
     WHERE  Alias1.Col1 != 'blah1' 
            AND Alias1.Col2 LIKE 'blah2' 
            AND ( Alias1.Col3 LIKE 'blah3' 
                   OR Alias1.Col3 LIKE 'blah4' ) 
            AND Alias1.Col4 = 'blah5' 
            AND Alias2.Col7 LIKE 'blah6' 
     ORDER  BY Alias1.Col6 DESC 
     LIMIT  50) 

    UNION 

    (SELECT Alias1 . *, 
            Alias2 . * 
     FROM   Table1 AS Alias1 
            LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
     WHERE  Alias1.Col1 != 'blah1' 
            AND Alias1.Col2 LIKE 'blah2' 
            AND ( Alias1.Col3 LIKE 'blah3' 
                   OR Alias1.Col3 LIKE 'blah4' ) 
            AND Alias1.Col4 = 'blah5' 
            AND Alias2.Col5 LIKE 'blah6' 
     ORDER  BY Alias1.Col6 DESC 
     LIMIT  50) 

    UNION 

    (SELECT Alias1 . *, 
            Alias2 . * 
     FROM   Table1 AS Alias1 
            LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
     WHERE  Alias1.Col1 != 'blah1' 
            AND Alias1.Col2 LIKE 'blah2'
            AND ( Alias1.Col3 LIKE 'blah3' 
                   OR Alias1.Col3 LIKE 'blah4' ) 
            AND Alias1.Col4 = 'blah5' 
     ORDER  BY Alias1.Col6 DESC 
     LIMIT  50)
   ) a

以下是给我错误(#1064 - 您的SQL语法中有错误;):

 SELECT COUNT(*) FROM (
   (SELECT Alias1 . *, 
            Alias2 . * 
     FROM   Table1 AS Alias1 
            LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
     WHERE  Alias1.Col1 != 'blah1' 
            AND Alias1.Col2 LIKE 'blah2' 
            AND ( Alias1.Col3 LIKE 'blah3' 
                   OR Alias1.Col3 LIKE 'blah4' ) 
            AND Alias1.Col4 = 'blah5' 
            AND Alias2.Col7 LIKE 'blah6' 
     ORDER  BY Alias1.Col6 DESC 
     LIMIT  50) a

    UNION 

    (SELECT Alias1 . *, 
            Alias2 . * 
     FROM   Table1 AS Alias1 
            LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
     WHERE  Alias1.Col1 != 'blah1' 
            AND Alias1.Col2 LIKE 'blah2' 
            AND ( Alias1.Col3 LIKE 'blah3' 
                   OR Alias1.Col3 LIKE 'blah4' ) 
            AND Alias1.Col4 = 'blah5' 
            AND Alias2.Col5 LIKE 'blah6' 
     ORDER  BY Alias1.Col6 DESC 
     LIMIT  50) b

    UNION 

    (SELECT Alias1 . *, 
            Alias2 . * 
     FROM   Table1 AS Alias1 
            LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
     WHERE  Alias1.Col1 != 'blah1' 
            AND Alias1.Col2 LIKE 'blah2'
            AND ( Alias1.Col3 LIKE 'blah3' 
                   OR Alias1.Col3 LIKE 'blah4' ) 
            AND Alias1.Col4 = 'blah5' 
     ORDER  BY Alias1.Col6 DESC 
     LIMIT  50) c
   ) z

5 个答案:

答案 0 :(得分:4)

将整个查询包装在括号中(使其成为子查询)并从中执行选择计数(*):

select count(*) from (YOUR HUGE QUERY UNION YOUR HUGE QUERY) a

答案 1 :(得分:1)

将整个查询包装为

SELECT COUNT(*) FROM (<Your query>)

答案 2 :(得分:1)

真的使用mysql_num_rows。对于联合,数据库无论如何都需要构建一个临时表,这是代价高昂的事情。即使可以在SQL中直接执行,也不会通过要求MySQL给你这个数字来节省很多。

无论如何,只要使用mysql_num_rows函数,数据就不会发送给PHP。

答案 3 :(得分:1)

您需要为列Col1指定一个唯一的别名,因为它在join中使用,需要唯一的列才能相互匹配。如果遇到重复的列,它将抛出错误1060。检查一下:Duplicate column names in SQL query

在您的情况下,MySQL与表1和表2中的Col1混淆,从而导致错误。使用完毕后,您可以SELECT COUNT(*) from YourQuery

答案 4 :(得分:1)

您需要为列Col1指定一个唯一的别名,因为它在join中使用,需要唯一的列才能相互匹配。如果遇到重复的列,它将抛出错误1060。检查一下:Duplicate column names in SQL query

在您的情况下,MySQL与表1和表2中的Col1混淆,从而导致错误。使用完毕后,您可以SELECT COUNT(*) from YourQuery