I must retrive all those who have sent messages to a given user and if the messages have not been read, return the number. I have two tables
users
| id_user| username | status |
| 1 | user1 | yes |
| 2 | user2 | yes |
| 3 | user3 | yes |
messages
| id_message |id_sender|id_dest| message | read |
| 1 | 1 | 2 | some text | yes |
| 2 | 3 | 2 | some text | no |
| 3 | 2 | 1 | some text | no |
and I have this query
select id_sender,username, count(distinct id_message) as nr_messages
FROM messages
INNER JOIN users
ON id_sender = id_user
WHERE id_dest=$dest and status='yes'
GROUP by id_sender
I can count the number of messages for each user but I can not know how many are unread. I must have a number if the messages are unread and 0 i the user do not have unread messages. for example for user2
sender: user3 (1 unread message)
sender: user1 (0 unred message)
messages table is ~2000000 rows
receiver_idin the schema as you have in query.sum(case when read = 'yes' then 0 else 1 end)...sum(read <>'yes')