I'm trying to understand how to count mysql row's according to HAVING.

```
SELECT COUNT(*),
  SUM(CASE WHEN sentby='$user_id' AND hiddenbysentby=0 THEN 1 ELSE 0 END) as sentbyuser,
  SUM(CASE WHEN sentto='$user_id' AND hiddenbysentto=0 THEN 1 ELSE 0 END) as senttouser
  FROM cb_users.user_pm
  WHERE title LIKE '%$search%'
  HAVING senttouser = 1 OR sentbyuser = 1

```

I want to count rows that are matching these criteria 

```
SUM(CASE WHEN sentby='$user_id' AND hiddenbysentby=0 THEN 1 ELSE 0 END)

SUM(CASE WHEN sentto='$user_id' AND hiddenbysentto=0 THEN 1 ELSE 0 END)

```

I have second function that display this data without COUNT(\*), and it works fine. 

But this query selects all rows no matter if hiddenbysentby = 1 or 0 AND hiddenbysentto = 1 or 0