0

I'm trying to get the ChannelGroups where the ChannelGroup has a count of 0 on its relation ChannelGroupUsers.

public function userEmptyChannelGroups()
    {
        return $this->getEntityManager()
            ->createQuery(
                'SELECT cg
                 FROM App:ChannelGroup cg
                 LEFT JOIN cg.channelGroupUsers cgu
                 WHERE COUNT(cgu) = 0'
            )
            ->getArrayResult();
    }

But I keep getting SQLSTATE[HY000]: General error: 1111 Invalid use of group function (500 Internal Server Error) upon trying the above.

What am I doing wrong?

Regentix
  • 293
  • 1
  • 4
  • 14

2 Answers2

0

If you want to find rows that don't have a connection to another table you can do something like this. In where there can be any column from table2.

select
    *
from table1 
left join table2
    on table1.id = table2.id
where table2.someColumn is null 
M. Kanarkowski
  • 2,155
  • 8
  • 14
0

You can't use an aggregate function, like COUNT in a WHERE clause. You can, however, use an aggregate function in a HAVING clause. See this question for more on the difference between WHERE and HAVING.

However, if you're just trying to get the rows in cg that don't have any joined rows from cgu, you can select where cgu.id is null.

For example:

SELECT cg
FROM App:ChannelGroup cg
LEFT JOIN cg.channelGroupUsers cgu
WHERE cgu.id IS NULL
Henry Woody
  • 14,024
  • 7
  • 39
  • 56