MYSQL Left Join COUNTS AND SUMS from multiple tables -
i have tried looking similar questions here in , have not found far. feel free tag duplicate or similar question 1 if you'd like.
i want join several columns display report.
i have 3 tables.
users
id name 1 chris 2 john 3 rick
inmessages
id content 1 hello1 2 hello2 3 response1 4 response2 5 hello3 6 hello4
outmessages
id userid inmessageid content currentrate 1 1 1 replyhello1 10 2 2 2 reply1hello2 10 3 3 2 reply2hello2 10 4 3 2 reply3hello2 10 5 1 3 replyresponse1 10 6 2 4 replyresponse2 10 7 1 5 replyhello3 4 8 3 6 replyhello4 4
and report i'd see like:
user inmessagescount outmessagescount rate10 rate4 chris 3 3 2 1 john 2 2 1 1 rick 2 3 2 1
i have tried existing queries like:
count(distinct inmessages.id) inmessagescount, count(distinct outmessages.id) outmessagescount
but stuck how make 1 rates.
thanks in advance!
you issue seems multiple joins creating duplicate records, hence needing distinct in current counts. can't use counting number of occurrences of non distinct field
as such tempted try this:-
count(distinct if(outmessages.currentrate = 10, outmessages.id, null) ) rate10, count(distinct if(outmessages.currentrate = 4, outmessages.id, null) ) rate4
ie, if id 10 use id, otherwise use null. count should (i think) count non null values.
Comments
Post a Comment