dbTalk Databases Forums  

How to Join

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss How to Join in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dot Net Daddy
 
Posts: n/a

Default How to Join - 03-12-2007 , 08:32 AM






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...


Reply With Quote
  #2  
Old   
Dave P
 
Posts: n/a

Default Re: How to Join - 03-12-2007 , 09:28 AM







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

Quote:
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...




Reply With Quote
  #3  
Old   
Dave P
 
Posts: n/a

Default Re: How to Join - 03-12-2007 , 10:42 AM



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 (AT) yahoo (DOT) com> wrote

Quote:
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...






Reply With Quote
  #4  
Old   
Ed Murphy
 
Posts: n/a

Default Re: How to Join - 03-12-2007 , 12:38 PM



Dot Net Daddy wrote:

Quote:
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.

Quote:
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.


Reply With Quote
  #5  
Old   
DaveP
 
Posts: n/a

Default Re: How to Join - 03-14-2007 , 02:11 PM



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 (AT) socal (DOT) rr.com> wrote

Quote:
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.



Reply With Quote
  #6  
Old   
--CELKO--
 
Posts: n/a

Default Re: How to Join - 03-16-2007 , 08:40 AM



Quote:
I have set up a database for movies.
Actually, you don't; such things already exist and you can download
them.

Quote:
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?

Quote:
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.

Quote:
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.

Quote:
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...



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.