Bug #54257 COUNT(DISTINCT tablename.column) makes the returned result only 1 record
Submitted: 5 Jun 2010 16:29 Modified: 8 Jun 2010 19:31
Reporter: Michael Garcia Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: Tests Severity:S7 (Test Cases)
Version:5.1.47-community OS:Any
Assigned to: CPU Architecture:Any
Tags: count, distinct, row

[5 Jun 2010 16:29] Michael Garcia
Description:
While trying to join a "virtual" column by LEFT OUTER JOIN I noticed the removing of :
, COUNT( DISTINCT topic.id ) AS topicCount, `topic`.`id` AS `t1_c0`
makes my query returns normally all the rows, otherwise I get only 1 record.

How to repeat:
Use the following dump :

-- phpMyAdmin SQL Dump
-- version 3.3.3
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1:3306

-- Generation Time: Jun 05, 2010 at 05:23 PM
-- Server version: 5.1.47
-- PHP Version: 5.3.2

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `web3cms`
--

-- --------------------------------------------------------

--
-- Table structure for table `w3_forum_sections`
--

CREATE TABLE `w3_forum_sections` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `parentId` int(10) unsigned NOT NULL DEFAULT '0',
  `name` tinytext NOT NULL,
  `description` text,
  `position` tinyint(3) unsigned NOT NULL,
  `isActive` tinyint(1) DEFAULT '1',
  `accessLevel` tinyint(3) unsigned DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `w3_forum_sections`
--

INSERT INTO `w3_forum_sections` VALUES(1, 0, 'sexion', '', 1, 0, 0);
INSERT INTO `w3_forum_sections` VALUES(2, 0, 'sexion2', '', 2, 0, 0);

-- --------------------------------------------------------

--
-- Table structure for table `w3_forum_topics`
--

CREATE TABLE `w3_forum_topics` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `sectionId` int(10) unsigned NOT NULL,
  `postedBy` int(10) unsigned NOT NULL,
  `created` int(10) unsigned NOT NULL,
  `replyCount` int(10) unsigned DEFAULT '0',
  `viewCount` int(10) unsigned DEFAULT '0',
  `closed` tinyint(1) DEFAULT '0',
  `sticky` tinyint(1) DEFAULT '0',
  `hasPoll` tinyint(1) DEFAULT '0',
  `isActive` tinyint(1) DEFAULT '1',
  `accessLevel` tinyint(3) unsigned DEFAULT '0',
  PRIMARY KEY (`id`,`sectionId`,`postedBy`),
  KEY `w3_forum_topics_FKIndex1` (`sectionId`),
  KEY `w3_forum_topics_FKIndex2` (`postedBy`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--
-- Dumping data for table `w3_forum_topics`
--

Then try (only 1 record) :

SELECT `t`.`id` AS `t0_c0` , `t`.`parentId` AS `t0_c1` ,  `t`.`name` AS `t0_c2` , `t`.`description` AS `t0_c3` , `t`.`position` AS `t0_c6` ,  `t`.`isActive` AS `t0_c7` , `t`.`accessLevel` AS `t0_c8` , COUNT( DISTINCT topic.id ) AS topicCount, `topic`.`id` AS `t1_c0` , `topic`.`sectionId` AS  `t1_c1` , `topic`.`postedBy` AS `t1_c2`
FROM `w3_forum_sections` `t`
LEFT
OUTER JOIN `w3_forum_topics` `topic` ON ( `topic`.`sectionId` = `t`.`id` )
ORDER BY position
LIMIT 0 , 300

and without "COUNT( DISTINCT topic.id ) AS topicCount" (2 records) :

SELECT `t`.`id` AS `t0_c0` , `t`.`parentId` AS `t0_c1` ,  `t`.`name` AS `t0_c2` , `t`.`description` AS `t0_c3` , `t`.`position` AS `t0_c6` ,  `t`.`isActive` AS `t0_c7` , `t`.`accessLevel` AS `t0_c8` , `topic`.`id` AS `t1_c0` , `topic`.`sectionId` AS  `t1_c1` , `topic`.`postedBy` AS `t1_c2`
FROM `w3_forum_sections` `t`
LEFT
OUTER JOIN `w3_forum_topics` `topic` ON ( `topic`.`sectionId` = `t`.`id` )
ORDER BY position
LIMIT 0 , 300
[7 Jun 2010 9:47] Sveta Smirnova
Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://dev.mysql.com/doc/ and the instructions on
how to report a bug at http://bugs.mysql.com/how-to-report.php

Please read at http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html how aggregate functions work.
[8 Jun 2010 19:31] Michael Garcia
Please tell me why COUNT prevent MySQL to display all records in the 2nd case.
(In the 2nd case it should at least returns 0 at these columns.)