Dernier e-mail reçu et nombre de messages non lus (reçus).
SELECT e.MAIL_NO, e.BIZ_ID, e.FROM_ADD, e.TO_ADD, e.EMAIL_SUBJECT,
DATE_FORMAT(e.UPDATED_DATE,'%d %b %y, %I:%i %p') AS DATE, e.MAIL_STATUS,
CONCAT(ufrom.USER_FIRST_NAME,' ',ufrom.USER_LAST_NAME) AS U_NAME,if(UNREAD_MESSAGE_COUNT is null,0,UNREAD_MESSAGE_COUNT) FROM EMAIL e
LEFT JOIN USER_CONFIG ufrom
ON ufrom.USER_ID = e.TO_ADD
left join (SELECT COUNT(*) AS UNREAD_MESSAGE_COUNT,TO_ADD FROM EMAIL_MESSAGE inner join EMAIL using (BIZ_ID)
WHERE MESSAGE_STATUS = 'U' group by TO_ADD) t on t.TO_ADD= e.TO_ADD
WHERE
e.TO_ADD=:TO_ADD
ORDER BY MAX(e.UPDATED_DATE) DESC, ufrom.USER_FIRST_NAME DESC
LIMIT 0, 10
E-mails envoyés et nombre de messages envoyés non lus :
SELECT e.MAIL_NO, e.BIZ_ID, e.FROM_ADD, e.TO_ADD, e.EMAIL_SUBJECT,
DATE_FORMAT(e.UPDATED_DATE,'%d %b %y, %I:%i %p') AS DATE, e.MAIL_STATUS,
CONCAT(ufrom.USER_FIRST_NAME,' ',ufrom.USER_LAST_NAME) AS U_NAME,if(UNREAD_MESSAGE_COUNT is null,0,UNREAD_MESSAGE_COUNT) FROM EMAIL e
LEFT JOIN USER_CONFIG ufrom
ON ufrom.USER_ID = e.FROM_ADD
left join (SELECT COUNT(*) AS UNREAD_MESSAGE_COUNT,FROM_ADD FROM EMAIL_MESSAGE inner join EMAIL using (BIZ_ID)
WHERE MESSAGE_STATUS = 'U' group by FROM_ADD) t on t.FROM_ADD= e.FROM_ADD
WHERE
e.FROM_ADD=:FROM_ADD
ORDER BY MAX(e.UPDATED_DATE) DESC, ufrom.USER_FIRST_NAME DESC
LIMIT 0, 10
Modifié :- Ajout de If count=null à 0 et ajout d'une jointure gauche pour garantir l'affichage des résultats.
édition 2 :modification de la sous-requête pour ne compter que les messages non lus envoyés à cet utilisateur.
Edit 3 :Mise à jour de la 1ère requête et ajout de la 2ème requête en fonction des résultats requis. Cela devrait faire l'affaire. Vous voulez probablement le premier.