-1

我想检索数据表格 3 即指定表格

  • UserDetail(Fname,Lname,User_id),
  • 电影(Movie_id,MovieName)
  • UserLikedMovies(User_id,Movie_id)

这样当用户输入特定的 Movie_id 时,用户 ID Fname Lname 从电影中输入用户详细信息 MovieName,这是我尝试过的

SELECT UserDetail.FName
FROM 
UserDetail UserDetail
INNER JOIN
UserLikedMovies UserLikedMovies
ON 
UserDetail.User_id = UserLikedMovies.User_id
INNER JOIN
(
    SELECT
        Movies.MovieName,
        Movies.Movie_id
    FROM
       Movies Movies
    INNER JOIN
        UserLikedMovies UserLikedMovies
        ON 
        Movies.Movie_id =  UserLikedMovies.Movie_id
    INNER JOIN
       UserDetail UserDetail
        ON 
        UserLikedMovies.User_id =  UserDetail.User_id
    WHERE
        Movies.Movie_id IN ( Select UserLikedMovies.Movie_id from UserLikedMovies where UserLikedMovies.Movie_id = 4)
) as ABC
ON UserLikedMovies.Movie_id = ABC.Movie_id
AND Movies.Movie_id = ABC.Movie_id
4

2 回答 2

0

这是一个非常简单的查询:

SELECT ud.`User_id`, ud.`Fname`, ud.`Lname`, m.`MovieName`
FROM `Movies` m 
RIGHT JOIN `UserLikedMovies` ulm ON ulm.`Movie_id` = m.`Movie_id`
LEFT JOIN `UserDetail` ud ON ud.`User_id` = ulm.`User_id`
WHERE m.`Movie_id` = 4

我不知道您为什么要为这个简单的任务使用可怕的子查询。

于 2013-10-13T18:48:26.823 回答
0

假设movie_id 为9,则查询如下。

SELECT User.Fname, User.Lname, (SELECT MovieName FROM Movies WHERE Movie_id = 9) AS MovieName FROM UserDetail User INNER JOIN UserLikedMovies Like ON User.User_id = Like.User_id WHERE Like.Movie_id = 9;

于 2013-10-13T18:52:06.367 回答