帮助sql选择语法

时间:2011-04-02 17:15:54

标签: c# asp.net mysql sql html

他来这里迷惑了他的一天! (从现在开始应该是我的手柄)

我很困惑,非常迷失。

我需要弄清楚如何制作一个select语句,以使FriendID与UserID

相关联

enter image description here

enter image description here

如您所见,FriendID与我的User表的UserID相关,因此如果FriendID = 2,则显示UserID 2详细信息。

我想尝试做的是,如果我是UserID 1我想显示我的所有朋友,我的用户ID是由会话设置的,

我需要做的是找出我的朋友是谁,所以选择来自朋友的FriendID,其中SessionID = UserID,然后知道如何找出谁在facebook表中属于谁以及显示他的名字,第二名和他的图片路径。

很混乱......

我的表格结构如下:

enter image description here

所以在我的select命令中总结一下我需要的东西:

找出谁是我的朋友(currentsession UserID有FriendID's) 显示与他自己的UserID相关的friendID的FirstName,SecondName,picturepath

示例:

我(userID = 1)有FriendID 2 = UserID 2所以从User中选择firstname,secondname,其中UserID = 2

我的桌面结构可能会产生关系问题而不确定。

我希望你能理解我的困惑:

对于那些看不到我的数据库图像

的人来说,

编辑

    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;

6 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

如果我正确理解了您的问题和数据模型,那么您要做的是查询通过成为朋友彼此连接的用户之间的多对多递归关系(即,通过Friends表行动就像多对多关系中的交叉实体一样。)

您可以通过将查询中的User表连接两次使用2个不同的别名来完成此操作,如下所示:

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 :(得分:1)

这样做:

    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)

这将返回您朋友的完整信息

答案 3 :(得分: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

答案 4 :(得分: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

答案 5 :(得分: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