There is a table of payments. It has a column users_id, the values ​​in which are repeated. It is necessary to calculate how many different users_id occurs in this column. Here is the code, but something does not output anything:

$query = "SELECT count (DISTINCT users_id) FROM payments"; $result = mysqli_query($link, $query); print_r($result); 
  • does not print anything. The query is valid, except for an extra space between the function name and the bracket. - Akina

1 answer 1

Option 1. In the question code, there is a space between count and (extra. The count (DISTINCT users_id) construct works.

 select count(distinct rev), count(distinct content) from docs; 

Option 2. Group and count the number of records in the grouped result:

 select count(*) as CONTENT_COUNT from (select content from docs group by content) a; 

Option 3: The same, but the subquery groups using distinct

 select count(*) as CONTENT_COUNT from (select distinct content from docs) a; 

An example you can try in SQLFiddle with schema code

 CREATE TABLE IF NOT EXISTS `docs` ( `id` int(6) unsigned not null, `rev` int(3) unsigned NOT NULL, `content` varchar(200) NOT NULL, PRIMARY KEY (`id`) ) DEFAULT CHARSET=utf8; INSERT INTO `docs` (`id`, `rev`, `content`) VALUES ('1', '1', 'A'), ('2', '1', 'B'), ('3', '2', 'B'), ('4', '3', 'B'); 
  • doesn't work anyway - Dmitry Php
  • Added the second option and examples. - StrelokMax
  • thanks, figured it out! - Dmitry Php