php - JOIN multiple MySQL tables in query -


i'm not sure if possible, or if join-fu isn't strong enough(it's pretty wimpy tell truth). have 3 tables tied uid. i'm trying information result of them 1 query, except i'm having trouble making sure result want.

users

========= users ========== | uid      | nickname    | -------------------------- | testusr1 | test user 1 | | testusr2 | test user 2 | | testusr3 | test user 3 | | testusr4 | test user 4 |  ============= gallery =========== | id | uid      | ext | profile | --------------------------------- | 1  | testusr1 | png | 1       | | 2  | testusr2 | jpg | 1       | | 3  | testusr3 | png | 1       | | 4  | testusr4 | png | 1       | | 5  | testusr4 | jpg | 0       |  ============= friends ============= | sender   | reciever    | status | ----------------------------------- | testusr1 | testusr3    | 0      | | testusr2 | testusr3    | 1      | | testusr2 | testusr1    | 1      | | testusr3 | testusr4    | 1      | 

what i'm trying of user's friends. friends in friends table status = 1. uid can either sender or reciever. in table above, testusr3's friends are: testusr2 , testusr4

from here want snag nickname users, , id gallery profile = 1 , uid = (that friend's id).

so far, query looks like:

$query = "select u.uid userid, g.id, g.ext, f.sender, f.reciever  friends f left join gallery g on g.uid = f.sender , g.profile = 1  left join users u on u.uid = f.sender  f.status = 1  , (f.sender = '$sentuid' or f.reciever = '$sentuid')"; 

but, labels of results f.sender...and i'm pretty sure g.profile = 1 isn't working. grab friends accurately though. appreciated.

untested solution

best place start matching records in single column, union. have uids need, in 1 place.

select f.uid, u.nickname, g.id (     (select reciever uid friends status=1 , sender='$sentuid')     union      (select sender uid friends status=1 , reciever='$sentuid') ) f left join gallery g on f.uid = g.uid , profile=1 left join users u on f.uid = u.uid 

side notes:

  • generally bad idea use char id field.
  • for performance reasons, may better off using more storage space, , doubling on 'friends' records. i.e.: 2 entries each friendship.

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -