使用聚合函数进行行计数

时间:2012-09-11 18:42:31

标签: php mysql

我在计算所有行时遇到问题。我的疑问是:

SELECT info.pid, Name, Addr, Phone, GROUP_CONCAT(URL) as URL
FROM info, images WHERE info.pid = images.pid AND place='$kraj' GROUP BY info.pid

我无法使用mysql_num_rows,因为即使没有结果,group_concat()也会创建一行(空值)。

我尝试将COUNT()置于查询中:

...GROUP_CONCAT(URL) as URL, COUNT(info.pid) as num FROM info...

但它为每个连接计算单独的行。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

为什么不过滤掉空值? :

SELECT info.pid, Name, Addr, Phone, GROUP_CONCAT(URL) as URL
  FROM info
  JOIN images
    ON info.pid = images.pid
 WHERE place = ...
   AND URL IS NOT NULL
 GROUP
    BY info.pid
;