i’m working on a private messaging system , it looks more like a social networks messaging . there is no title and related pms are shown like forum threads .
i have two table to keep tracks of the pms
pm_sessions : pm_session , sender , receiver, pm_counter
pm : id , pm_session , text , date , sender
pm_session works like a wrapper for pm
now in the user profile i want to show list of the users whos been sending or receiving pm from current user
the problem is i don’t want to show receiver and sender users separately , i want to have all of the users whos been talking to my guy in one list
here is what i got in mind ,and i now it doesn’t work like this with all IF statements
$current_user = $_session['userid'];
$query = "
select pms.sender , pms.receiver,
count(pms.otheruser) as total_dialogs_with_this_user ,
u.username as other_user_name , u.id as other_user_id
from pm_session pms
//// joining to the users table on the other user id
if($current_user = pms.sender)
JOIN users u on pms.receiver= u.id
if($current_user = pms.receiver)
JOIN users u on pms.sender = u.id
WHERE pms.reciver = $current_user || pms.sender = $current_user
/// grouping by other user id
if($current_user = pms.sender)
group by pms.receiver
if($current_user = pms.receiver)
group by pms.sender ";
i expect results to be something like
HELLO max ... you have been talking to :
ross... 3 dialogs
joey ... 4 dialogs
chandler ... 5 dialogs
i can easily write this with two query , one for receiver and one for senders
but like i said i dont want to separate them
1 Answer