![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hello, I have set up a database for movies. In one table (Movies) I have movie names, and production years, and also genres. In another table (Directors), I keep the directors and the movies they directed. Another table (People) keeps the names of the people. Everybody will have a unique ID. I have created a query like below to show the name and production year of the movie, the director name and the genre of the movie. Genres are also defined in a tabled called Genres. SELECT Movies.Name, Movies.Year, People.Name AS Director, Genres.Genre FROM Movies INNER JOIN Directors ON Movies.ID = Directors.MovieID INNER JOIN Genres ON Movies.Genre = Genres.ID INNER JOIN People ON Directors.ID = People.ID WHERE (Movies.ID = @MoviesID) The problem is that it does not return any result. What might be the problem? Thanks in advance... |
#3
| |||
| |||
|
|
Where you are joining on Directors that 1st Join There looks like directors should be the people table... Dave P "Dot Net Daddy" <cagriandac (AT) gmail (DOT) com> wrote in message news:1173709932.900195.198150 (AT) j27g2000cwj (DOT) googlegroups.com... Hello, I have set up a database for movies. In one table (Movies) I have movie names, and production years, and also genres. In another table (Directors), I keep the directors and the movies they directed. Another table (People) keeps the names of the people. Everybody will have a unique ID. I have created a query like below to show the name and production year of the movie, the director name and the genre of the movie. Genres are also defined in a tabled called Genres. SELECT Movies.Name, Movies.Year, People.Name AS Director, Genres.Genre FROM Movies INNER JOIN Directors ON Movies.ID = Directors.MovieID INNER JOIN Genres ON Movies.Genre = Genres.ID INNER JOIN People ON Directors.ID = People.ID WHERE (Movies.ID = @MoviesID) The problem is that it does not return any result. What might be the problem? Thanks in advance... |
#4
| |||
| |||
|
|
I have set up a database for movies. In one table (Movies) I have movie names, and production years, and also genres. In another table (Directors), I keep the directors and the movies they directed. |
|
Another table (People) keeps the names of the people. Everybody will have a unique ID. I have created a query like below to show the name and production year of the movie, the director name and the genre of the movie. Genres are also defined in a tabled called Genres. SELECT Movies.Name, Movies.Year, People.Name AS Director, Genres.Genre FROM Movies INNER JOIN Directors ON Movies.ID = Directors.MovieID INNER JOIN Genres ON Movies.Genre = Genres.ID INNER JOIN People ON Directors.ID = People.ID WHERE (Movies.ID = @MoviesID) The problem is that it does not return any result. What might be the problem? |
#5
| |||
| |||
|
|
Dot Net Daddy wrote: I have set up a database for movies. In one table (Movies) I have movie names, and production years, and also genres. In another table (Directors), I keep the directors and the movies they directed. Based on your query, I assume that you are /not/ making the classic violation of 1NF, which would look like: ID | ListOfMovieIDs ---+--------------- 1 | 1,2 2 | 3 3 | 3 but rather you have done it correctly: ID | MovieID ---+-------- 1 | 1 1 | 2 2 | 3 3 | 3 Personally, I would rename the ID column to DirectorID. In particular, some tools (e.g. the Smart Linking option in Crystal Reports) will give more useful results if you do this. Similarly for the ID columns in the other tables. Another table (People) keeps the names of the people. Everybody will have a unique ID. I have created a query like below to show the name and production year of the movie, the director name and the genre of the movie. Genres are also defined in a tabled called Genres. SELECT Movies.Name, Movies.Year, People.Name AS Director, Genres.Genre FROM Movies INNER JOIN Directors ON Movies.ID = Directors.MovieID INNER JOIN Genres ON Movies.Genre = Genres.ID INNER JOIN People ON Directors.ID = People.ID WHERE (Movies.ID = @MoviesID) The problem is that it does not return any result. What might be the problem? Build up the query one level at a time: SELECT Movies.Name, Movies.Year FROM Movies WHERE Movies.ID = @MoviesID If this returns zero rows, then @MoviesID is not in the Movies table. SELECT Movies.Name, Movies.Year, Director.ID as DirectorID FROM Movies INNER JOIN Directors ON Movies.ID = Directors.MovieID WHERE Movies.ID = @MoviesID If this returns zero rows, then Movies.ID is not in the Directors table. SELECT Movies.Name, Movies.Year, People.Name as Director FROM Movies INNER JOIN Directors ON Movies.ID = Directors.MovieID INNER JOIN People ON Directors.ID = People.ID WHERE Movies.ID = @MoviesID If this returns zero rows, then Directors.ID is not in the People table. Fix all such cases, then add a foreign-key constraint to prevent it from happening again. SELECT Movies.Name, Movies.Year, People.Name as Director, Genres.Genre FROM Movies INNER JOIN Directors ON Movies.ID = Directors.MovieID INNER JOIN People ON Directors.ID = People.ID INNER JOIN Genres ON Movies.Genre = Genres.ID WHERE Movies.ID = @MoviesID If this returns zero rows, then Movies.Genre is not in the Genres table. Fix and add constraint. Alternatively, you can replace any/all of the INNER JOINs with LEFT OUTER JOINs. You will then get NULLs from that branch of the join tree, e.g. if Movies.ID is not in the Directors table then anything you attempt to get from Directors *or* People will be NULL. COALESCE(SomeField,'DefaultValue') may be of interest. |
#6
| |||||
| |||||
|
|
I have set up a database for movies. |
|
In one table (Movies) I have movie names, and production years, and also genres. In another table (Directors), I keep the directors and the movies they directed. |
|
Another table (People) keeps the names of the people. Everybody will have a unique ID. |

|
I have created a query like below to show the name and production year of the movie, the director name and the genre of the movie. |
|
SELECT Movies.Name, Movies.Year, People.Name AS Director, Genres.Genre FROM Movies INNER JOIN Directors ON Movies.ID = Directors.MovieID INNER JOIN Genres ON Movies.Genre = Genres.ID INNER JOIN People ON Directors.ID = People.ID WHERE (Movies.ID = @MoviesID) The problem is that it does not return any result. What might be the problem? Thanks in advance... |
![]() |
| Thread Tools | |
| Display Modes | |
| |