This is a discussion on How to Join within the SQL Server forums, part of the Microsoft SQL Server category; --> Hello, I have set up a database for movies. In one table (Movies) I have movie names, and production ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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... |
| |||
| Where you are joining on Directors that 1st Join There looks like directors should be the people table... Dave P "Dot Net Daddy" <cagriandac@gmail.com> wrote in message news:1173709932.900195.198150@j27g2000cwj.googlegr oups.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... > |
| |||
| it looks like you have many different types of peopls in ur peoples table directors and select * from Movies M (nolock) join peoples d (nolock) d.directorsGroup=m.DirectorsGroup join peoples. a (nolock) a.ActorsGroup=m.ActorsGroup join GenRes G (nolock) g.Genres=m.Genres above is a sample join of somthing of what your data may look like h "Dave P" <analizer1@yahoo.com> wrote in message news:5keJh.6857$M65.1830@newssvr21.news.prodigy.ne t... > > Where you are joining on Directors that 1st Join There > looks like directors should be the people table... > > > Dave P > > > "Dot Net Daddy" <cagriandac@gmail.com> wrote in message > news:1173709932.900195.198150@j27g2000cwj.googlegr oups.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... >> > > |
| |||
| 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. |
| |||
| Im Confused about ur tables movies MovieId(identity), other columns, DirectorId (from Directors), genresId (from Genres) if your table is not designd somewhat like the above, gonna be hard to link the child tables(ref tables, directors, genres) movie movieid (identy) title genres (id from genres) could be identy in genres or another unique id director (id from Director) could be identy in genres or another unique id yearmade Studio etc hope the above helps DaveP "Ed Murphy" <emurphy42@socal.rr.com> wrote in message news:45f59e2c$0$16735$4c368faf@roadrunner.com... > 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. |
| ||||
| >> I have set up a database for movies. << Actually, you don't; such things already exist and you can download them. >> 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. << If a movie can have more than one director, then where is the relationship table? >> Another table (People) keeps the names of the people. Everybody will have a unique ID. << It is nice to know you do not consider Directors to be people and put them ina separate table Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Sample data is also a good idea, along with clear specifications. It is very hard to debug code when you do not let us see it. >> 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. << What you posted is completely wrong. The data element names are too vague to be useful and involve reserved words. You are so far off base, you even have the magical, universal id column which changes meaning from table to table! Tell me that you did not use an IDENTITY in all your tables for this. CREATE TABLE Movies (<<industry standard if>>, release_year INTEGER NOT NULL, genre_code CHAR(10) NOT NULL, etc.); CREATE TABLE Personnel (..) -- SAG number as id? CREATE TABLE Crew (..) -- includes role played by personnel on a movie Start over with a relational design or your teacher will give you a really bad grade. > > 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... |