数据库查询

时间:2014-03-15 18:12:08

标签: mysql sql database subquery

所以,我正在为我的项目创建这个电影测验应用程序,我很难找出它的查询。如果你能帮助我,我将不胜感激。

所以,架构如下所示。 表1:电影 值 - id(主键)标题,年份,导演,电影

表2:明星 values - id(主键)first_name,last_name

表3:stars_in_movie 值 - star_id,movie_id

以下是我需要查询的问题。

Who directed the movie X? I figuered this one out. 

从stars_in_movies内连接星中选择first_name,last_name,其中movie_id =%s和star_id = id;

Who directed the star X?
Who did not directed the star X?
Which star appears in both movies X and Y?
Which star did not appear in the same movie with the star X?
Who directed the star X in year Y? 

感谢您的帮助。 谢谢。

1 个答案:

答案 0 :(得分:0)

虽然您可能需要处理mysql语法,但这里有一些广泛的提示。您可以使用mysql语法而不是IN来使用内部联接来更有效地完成此操作。

select director from movies where movieid in (select movieid from star_in_movie where starid in (select starid from stars where first_name like ....  ))

select director from movies where movieid NOT in (select movieid from star_in_movie where starid in (select starid from stars where first_name like ....  ))

select first_name, last_name from stars where (starid in (select starid from star_in_movie where movieid in (select movieid from movies where title = X)))
And 
(starid in (select starid from star_in_movie where movieid in (select movieid from movies where title = X)))

我会根据上面的提示将其他两个留给你的想象力。如果您仍然需要帮助,请告诉我。 mysql中内连接的语法必须类似于:X.colname = Y.colname上的X内连接Y.