0

Here he comes to confuse his day! (should be my handle from now on)

Im confused and very much lost.

I need to figure out how to make a select statement for getting FriendID to relate to there UserID

enter image description here

enter image description here

As you can see FriendID relates to the UserID of my User table, so if FriendID = 2 then display UserID 2 details.

What im trying to do is if im UserID 1 I want to display all my friends, my userid is set by a session,

What I need to do is find out who my friends are so Select FriendID from freinds where SessionID = UserID then some how figure out who friendsID belongs to in the User table and display his firstname, secondname and his picturepath.

Very confusing...

My table structure looks like this:

enter image description here

So to sum it up what I need in my select command:

To find out who im friends with (currentsession UserID has FriendID's) Display FirstName, SecondName, picturepath of friendID related to his own UserID

Example:

I (userID=1) have FriendID 2 = UserID 2 so select firstname, secondname from User where UserID=2

I could have relational problems with my table structure tho not sure.

I hope u can understand my confusion:

EDIT for those that cant see the images of my db

    SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;

SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;

SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';



CREATE SCHEMA IF NOT EXISTS `gymwebsite2` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;

USE `gymwebsite2` ;



-- -----------------------------------------------------

-- Table `gymwebsite2`.`User`

-- -----------------------------------------------------

CREATE  TABLE IF NOT EXISTS `gymwebsite2`.`User` (

  `UserID` INT NOT NULL AUTO_INCREMENT ,

  `Email` VARCHAR(245) NULL ,

  `FirstName` VARCHAR(45) NULL ,

  `SecondName` VARCHAR(45) NULL ,

  `DOB` VARCHAR(45) NULL ,

  `Location` VARCHAR(45) NULL ,

  `Aboutme` VARCHAR(1045) NULL ,

  `username` VARCHAR(45) NULL ,

  `password` VARCHAR(45) NULL ,

  PRIMARY KEY (`UserID`) )

ENGINE = InnoDB;





-- -----------------------------------------------------

-- Table `gymwebsite2`.`WallPosting`

-- -----------------------------------------------------

CREATE  TABLE IF NOT EXISTS `gymwebsite2`.`WallPosting` (

  `idWallPosting` INT NOT NULL AUTO_INCREMENT ,

  `UserID` INT NOT NULL ,

  `Wallpostings` VARCHAR(2045) NULL ,

  PRIMARY KEY (`idWallPosting`) ,

  INDEX `fk_WallPosting_User` (`UserID` ASC) ,

  CONSTRAINT `fk_WallPosting_User`

    FOREIGN KEY (`UserID` )

    REFERENCES `gymwebsite2`.`User` (`UserID` )

    ON DELETE NO ACTION

    ON UPDATE NO ACTION)

ENGINE = InnoDB;





-- -----------------------------------------------------

-- Table `gymwebsite2`.`Pictures`

-- -----------------------------------------------------

CREATE  TABLE IF NOT EXISTS `gymwebsite2`.`Pictures` (

  `idPictures` INT NOT NULL AUTO_INCREMENT ,

  `UserID` INT NOT NULL ,

  `picturepath` VARCHAR(1045) NULL ,

  PRIMARY KEY (`idPictures`) ,

  INDEX `fk_Pictures_User1` (`UserID` ASC) ,

  CONSTRAINT `fk_Pictures_User1`

    FOREIGN KEY (`UserID` )

    REFERENCES `gymwebsite2`.`User` (`UserID` )

    ON DELETE NO ACTION

    ON UPDATE NO ACTION)

ENGINE = InnoDB;





-- -----------------------------------------------------

-- Table `gymwebsite2`.`Friends`

-- -----------------------------------------------------

CREATE  TABLE IF NOT EXISTS `gymwebsite2`.`Friends` (

  `idFriends` INT NOT NULL AUTO_INCREMENT ,

  `UserID` INT NOT NULL ,

  `FriendID` INT NOT NULL ,

  PRIMARY KEY (`idFriends`) ,

  INDEX `fk_Friends_User2` (`FriendID` ASC) ,

  INDEX `fk_Friends_User1` (`UserID` ASC) ,

  UNIQUE INDEX `FriendID_UNIQUE` (`FriendID` ASC) ,

  CONSTRAINT `fk_Friends_User2`

    FOREIGN KEY (`FriendID` )

    REFERENCES `gymwebsite2`.`User` (`UserID` )

    ON DELETE NO ACTION

    ON UPDATE NO ACTION,

  CONSTRAINT `fk_Friends_User1`

    FOREIGN KEY (`UserID` )

    REFERENCES `gymwebsite2`.`User` (`UserID` )

    ON DELETE NO ACTION

    ON UPDATE NO ACTION)

ENGINE = InnoDB;







SET SQL_MODE=@OLD_SQL_MODE;

SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;

SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
3
  • your images did not loaded. please describe your table structure. Commented Apr 2, 2011 at 17:20
  • hmm strange? i will re-edit one sec Commented Apr 2, 2011 at 17:22
  • 1
    Images problem solved. You can find solution in my answer. Commented Apr 2, 2011 at 17:26

6 Answers 6

2
select u.FirstName, u.SecondName, p.picturePath
from User u
join Friends f
  on f.FriendId = u.UserId
join Pictures p
  on p.UserId = u.UserId
where f.UserId = SessionId ( <-- sessionId is your id)
Sign up to request clarification or add additional context in comments.

1 Comment

Note that changing the join on Pictures to a left join would be preferred if not all users have pictures.
1
SELECT u.firstName, u.secondName, p.picturepath
FROM User as u, Friends as f, Pictures as p
WHERE f.UserID = $sessionID 
AND u.UserID = f.FriendID
AND p.UserId = f.FriendID

2 Comments

You wanted the picturepath ... you need to join the Pictures table as well. The as is optional. It's fairly old school, I just think it reads well.
yeah sorry brian your fast at replying once i noticed i deleted my comment, I thought i was seeing double at first
1

do like this :

    SELECT User.*, Pictures.PicturePath
           FROM (User INNER JOIN Friends 
                ON User.UserId = Friends.FriendId) table
                INNER JOIN ON table.UserId = Pictures.UserId
           WHERE Friends.UserId = 1 // this is your id (SessionId)

this will return full information of your friends

Comments

1

If I understand your question and data model correctly, what you are trying to do is to query a many-to-many recursive relationship between users which are connected by being friends to each other (i.e, through the Friends table which acts like the intersecting entity in a many-to-many relationship).

You can do this by joining the User table in your query twice with 2 different aliases, like this:

select 
    u2.UserID,
    u2.FirstName,
    u2.SecondName,
    p.picturepath
from User u1 -- This gets me
join Friends f on u1.UserID = f.UserID -- This gets my friends
join User u2 on f.FriendID = u2.UserID -- This gets my friends info
join Pictures p on p.UserID = u2.UserID -- This gets my friends pics
where u1.UserID = 1  -- ...or whatever; don't actually hardcode "1"!

2 Comments

what about picturepath in a different table?
@Garrith: Sorry, an oversight! Corrected now
1
    SELECT x.FirstName,
           x.SecondName,
           x.Email,
           z.picturepath
      FROM ( Friends AS x,
             User AS y
           )
 LEFT JOIN Pictures AS z
        ON x.FriendID = z.UserId
     WHERE x.UserID = 1  # <- this is determined by the session ID
       AND x.FriendID = y.UserID

Comments

0
SELECT 
   u.FirstName, 
   u.SecondName, 
   p.picturepath
FROM User as u 
  INNER JOIN Friends as f
  ON u.UserId = f.FriendId
    INNER JOIN Pictures as p
    ON u.UserId = p.UserId
WHERE f.UserId = 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.