Jump to content

Recommended Posts

Hey all, I have a little bit of an issue with a query. I have 2 tables users and network

 

users:

------

userid

username

main_image

level

...

 

network:

------

nid

userid

friendid

ap

 

When a user adds another user into their network the sender is userid and the reciever is friendid. When it is approved each person shows up in one another's network. So the users.userid can either be network.userid or network.friendid

 

Here is what I want...1 query that outputs the username, main_image, level, and nid of the other person in the network.

 

Here is what I have so far:

SELECT n.nid, n.userid, n.friendid, u.username, u.level, u.main_image FROM network n LEFT JOIN users u on (u . userid = n . userid) WHERE (n.friendid = $userid OR n.userid = $userid) AND ap = 1

 

The problem is, is that if mine is the userid it shows my name and main_image. Is there a way to "scan" either userid or friendid and take the oposite one? I think I'm pretty close here.

 

Thanks!

-Chris

Link to comment
https://forums.phpfreaks.com/topic/50982-solved-semi-complex-query/
Share on other sites

maybe this:

 

SELECT network.nid
     , network.userid
     , network.friendid
     , IF (owner.userid IS NULL, friend.username, owner.username) AS username
     , IF (owner.userid IS NULL, friend.level, owner.level) AS level
     , IF (owner.userid IS NULL, friend.main_image, owner.main_image) AS main_image
FROM network
     LEFT JOIN users owner ON network.userid = owner.userid
     LEFT JOIN users friend ON network.userid = friend.userid) 
WHERE (n.friendid = $userid OR n.userid = $userid) AND ap = 1

hey thanks for the reply. I just had to change your uery around for it to "work" but I'm still getting the same results.

 

SELECT network.nid
     , network.userid
     , network.friendid
     , IF (owner.userid = 1, friend.username, owner.username) AS username
     , IF (owner.userid = 1, friend.level, owner.level) AS level
     , IF (owner.userid = 1, friend.main_image, owner.main_image) AS main_image
FROM network
     LEFT JOIN users owner ON network.userid = owner.userid
     LEFT JOIN users friend ON network.userid = friend.userid 
WHERE (network.friendid = 1 OR network.userid = 1) AND ap = 1

 

I'm on MySQL 4.1.14 if that helps

SELECT network.nid
    , network.userid
    , network.friendid
    , if (network.userid = 1, friend.username, owner.username) AS username
    , if (network.userid = 1, friend.main_image, owner.main_image) AS main_image
    , if (network.userid = 1, friend.level, owner.level) AS level
FROM network
    INNER JOIN users AS owner ON network.userid = owner.userid
    INNER JOIN users AS friend ON network.friendid = friend.userid 
WHERE ( network.userid = 1 OR network.friendid = 1 ) AND ap = 1;

 

SELECT network.nid
    , network.userid
    , network.friendid
    , if (network.userid = $userid, friend.username, owner.username) AS username
    , if (network.userid = $userid, friend.main_image, owner.main_image) AS main_image
    , if (network.userid = $userid, friend.level, owner.level) AS level
FROM network
    INNER JOIN users AS owner ON network.userid = owner.userid
    INNER JOIN users AS friend ON network.friendid = friend.userid 
WHERE ( network.userid = $userid OR network.friendid = $userid ) AND ap = $ap;

 

EDIT: though the query looks a lot different the primary problem was network.userid in the second LEFT JOIN should be network.friendid and I figured an INNER JOIN is more appropriate since both ids are required.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.