Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Why select count distinct return zero

Tags:

mysql

I would like to know why select count distinct return zero result. I have also checked various answers at here but it's not answer for my case. MySQL version 5.6 Link to check http://sqlfiddle.com/#!9/276302/3/0

Sample schema:

CREATE TABLE IF NOT EXISTS `employees` (
  `id` int(6) unsigned NOT NULL,
  `name` varchar(3) NOT NULL,
  `salary` varchar(200) NOT NULL,
  PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `employees` (`id`, `name`, `salary`) VALUES
  ('1', 'a', 6),
  ('2', 'b', 5),
  ('3', 'c', 5),
  ('4', 'd', 4);

Query:

SELECT COUNT(DISTINCT(salary))
FROM employees;

Guys, do you have any idea ?

like image 875
ToujouAya Avatar asked Apr 14 '26 22:04

ToujouAya


2 Answers

In my opinion it is a bug of this specific version of MySQL 5.6.

But it will work for MariaDB or MySQL 5.7

The other answers suggest that it is the problem with datatype.But if we remove DISTINCT:

SELECT COUNT(salary)
FROM employees;
-- 4

DBFiddle Demo MySQL 5.6

like image 60
Lukasz Szozda Avatar answered Apr 16 '26 10:04

Lukasz Szozda


select count(ds) from (SELECT distinct(salary) as ds
FROM employees) as s;
like image 37
godot Avatar answered Apr 16 '26 11:04

godot