mysql - Display the number of replies to a message with inner joins in PHP -
anything beyond basic sql statements, not strong point.
i have messaging system i'm coding kind of facebook's instead of separate messages, reply 1 main message, , messages you're participating in displayed in 1 spot.
mail table:
- mailid
- mailuser: person sent message
- mailto
- mailunread: 1 unread
- mailsubject
- mailbody
- mailcurrency
- mailtime
- maildeleted: 1 deleted
relevant fields in mailreply table:
- mailreplyid
- mailreplyuser: user that's replying
- mailreplydeleted: 1 deleted
- mailreplyto: id of message replied to
original coded didn't count number of replies , worked fine, decided helpful include number.
this had:
select a.mailid, a.mailuser, a.mailto, b.username usernamea, c.username usernameb mail a, users b, users c (a.mailuser = b.id or a.mailuser = c.id) , (a.mailto = '".$id."' or a.mailuser = '".$id."') , a.maildeleted = '0' group a.mailid order a.mailid desc this attempt:
select a.mailid, a.mailuser, a.mailto, b.username usernamea, c.username usernameb mail a, users b, users c left join mailreply d on d.mailreplyto = a.mailid (a.mailuser = b.id or a.mailuser = c.id) , (a.mailto = '".$id."' or a.mailuser = '".$id."') , a.maildeleted = '0' group a.mailid order a.mailid desc thanks
try this:
select a.mailid, a.mailuser, a.mailto, b.username fromusername, c.username tousername, count(d.mailreplyid) mailcount mail inner join users b on b.id=a.mailuserid inner join users c on c.id=a.mailto left join mailreply d on d.mailreplyto = a.mailid (a.mailto = '".$id."' or a.mailuser = '".$id."') , a.maildeleted = '0' group a.mailid order a.mailid desc you need join users table on each side of message usernames. think confusing clause check if current user sender or recipient join specification, that's reason using inner join syntax rather separating tables commas , putting join spec in clause.
you group mail id correctly did, , add in count column on mailreply table. table left joined, right about, return messages count of 0 replies.
hope sorts - there!
james
Comments
Post a Comment