To get the latest row in MySQL, you need to use a join
or correlated subquery:
SELECT id, user_receiver, user_sender, post_id, action, date, is_read
FROM notification n
WHERE user_receiver=$ses_user and
date = (select max(date)
from notification n2
where n2.user_sender = n.user_sender and
n2.action = n.action and
n2.post_id = n.post_id and
n2.is_read = n.is_read
)
order by date desc;
In other databases, you would simply use the row_number()
function (or distinct on
in Postgres).
EDIT:
For the biggest id:
SELECT id, user_receiver, user_sender, post_id, action, date, is_read
FROM notification n
WHERE user_receiver=$ses_user and
id = (select max(id)
from notification n2
where n2.user_sender = n.user_sender and
n2.action = n.action and
n2.post_id = n.post_id
)
order by date desc;
If you want the number of rows where isread = 1
, then you can do something like:
SELECT sum(is_read = 1)
FROM notification n
WHERE user_receiver=$ses_user and
id = (select max(id)
from notification n2
where n2.user_sender = n.user_sender and
n2.action = n.action and
n2.post_id = n.post_id
);