$sSql = "SELECT COUNT(DISTINCT `tsu`.`id`) AS `count`
FROM `" . $this->_sPrefix . "users` AS `tsu`
INNER JOIN `" . $this->_sPrefix . "entries` AS `tse`
ON `tsu`.`id`=`tse`.`subscriber_id`
AND `tse`.`subscriber_type`='" . BX_DOL_SBS_TYPE_VISITOR . "'
WHERE 1
GROUP BY `tsu`.`id`
LIMIT 1";
Instead of counting the entries, it only returns 1. If I remove the group BY clause then it works. How can I fix the GROUP BY to make it work better?
The WHERE 1
does nothing, so it can be removed.
The GROUP BY
can also be removed since you are not grouping by anything, your COUNT DISTINCT
is on the whole table, no?
As Orbling points out, the LIMIT 1
can be removed too because COUNT DISTINCT
returns only one value by definition.
Does it do what you want then?
You are grouping by tsu
.id
.
Each individual tsu
.id
group will by definition only have 1 distinct tsu
.id
in the group what do you expect it to return?
SELECT u.id, COUNT(e.id)
FROM users AS u
INNER JOIN entries AS e ON e.subscriber_id = u.id
WHERE e.subscriber_type = 'BX_DOL_SBS_TYPE_VISITOR'
GROUP BY u.id