Question : Join Syntax - 3 Tables

Hi Folks,

I've been struggling with this one all day, and I've reached a point where nothing I read is making any sense!

Here's the problem - I have 3 tables I need to 'connect' together in a query (screenshot of schema attached).  I need to return records ('status updates') from the table 'statusupdates', but only for users where there is a connection (determined by the 'connections' table).  

The 'matching' fields are sUserID in the connections table against wUserID in the statusupdates table.  The user for which we want display these records is wUserID (the value of which is the currently logged in user).  For example, in the connections table, we might have a wUserID of 1, and a sUserID of 2.  The next row may have a wUserID of 1 (again) and a sUserID of 3.  This would mean the currently logged in user (1) should see all results from the statusupdates table where the wUserID is either 2 or 3.

Does that make sense? (I'm at a point where I'm not too sure myself!)

I've pasted my existing query below, however this basically just shows all the status updates and doesn't currently look for a connection.

Any help with this one would be much appreciated, and save what little hair I have left! ;-)
(FYI - I'm using MySQL)

1:
2:
3:
4:
5:
SELECT s.sID, s.wUserID, s.sStatus, s.sCreatedDate, u.FirstName, u.Surname
FROM statusupdates s
LEFT JOIN users u
ON s.wUserID=u.wUserID
ORDER BY s.sCreatedDate DESC

Answer : Join Syntax - 3 Tables

create table #connections (wuserid int, suserID int, cName varchar(128))
create table #status (suid int, wuserid int, sstatus varchar(128))
create table #users (wuserid int, userName varchar(128))
insert into #users values(1, 'user1')
insert into #users values(2, 'user2')
insert into #users values(3, 'user3')
Is this what you need?
Please provide more sample data for inserts below to illustrate what you need
insert into #connections values(1,1, '1-1 connection')
insert into #connections values(1,2, '1-2 connection')
insert into #connections values(2,3, '3-3 connection')

insert into #status values(10, 1,'user1 active')
insert into #status values(20, 2,'user2 hold')
insert into #status values(30, 3,'user3 deleted')

SELECT *
FROM  #users u
      left join #connections c on  u.wUserID=c.wUserID
      left join #status s on c.suserid = s.wUserID
      order by u.wuserid, c.suserid
Random Solutions  
 
programming4us programming4us