Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine many notifications to one

I'm re-creating my notification system to make it able to combine many notifications to one. Instead of showing 9 rows that say "Adam has answered your question", it could say "9 people have answered your question".

My current notification table looks like this:

notification_id (PK) | recipient_id | sender_id | type | foreign_id | date | viewed

Recipient_id is the one to get the notification. Sender_id is the one it came from. Type is what kind of notification, so I know what it should say, "xx has answered" or "xx liked your post". Foreign_id could be the ID of a question.

Which would be the best way to create this "combine" system? Should I update the notification when there is a new answer and say that another person has answered too, or should the query maybe count how many there is of the same foreign_id. What do you think?

like image 802
lawls Avatar asked Dec 06 '25 06:12

lawls


1 Answers

Try this:

SELECT foreign_id, SUM(IF (TYPE= 'like', 1, 0)) likecnt, 
       SUM(IF(TYPE = 'answer', 1, 0)) anscnt 
FROM notification 
GROUP BY foreign_id

EDIT: Check this

SELECT n.foreign_id, n.sender_id
FROM notifications n 
INNER JOIN (SELECT foreign_id, MAX(DATE) DATE 
            FROM notifications 
            GROUP BY foreign_id, user_id
           )  a ON n.foreign_id = a.foreign_id AND n.date = a.date
like image 71
Saharsh Shah Avatar answered Dec 07 '25 19:12

Saharsh Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!