Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
If you have a very simple query with just one condition
Steps to reproduce the issue:
Just run this code
$query = db_select('users', 'u');
$query->addExpression('COUNT(name)');
$query->condition('uid', 0, '>');
$query->execute();
dsm((string) $query);
$countQuery = $query->countQuery();
dsm((string) $countQuery);
Expected behavior:
SELECT COUNT(*) AS expression
FROM
(SELECT COUNT(name) AS expression
FROM
{users} u
WHERE (uid > :db_condition_placeholder_0) ) subquery
Current behavior:
SELECT COUNT(*) AS expression
FROM
(SELECT 1 AS expression
FROM
{users} u
WHERE (uid > :db_condition_placeholder_0) ) subquery
The proper way to fix this is kind of hard. The problem is that countQuery() assumes that if a expression isn't added
via groupBy it have to be removed, though it's valid sql to have just the expression without any further groupBy.
Comments
Comment #1
nestor.mata CreditAttribution: nestor.mata commentedSo, what is your advice in this situation?
Comment #2
eddyger CreditAttribution: eddyger commentedhow about adding a $zero-out boolean parameter (default to true) in SelectQuery::countQuery() ?
Comment #2.0
eddyger CreditAttribution: eddyger commentedformatting
Comment #11
catchDuplicate of #1258000: SelectQuery::countQuery() inappropriately removes SQL expressions.