Reply to Re: How to Join

Your name:

Reply:


Posted by Ed Murphy on 03/12/07 18:38

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.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация