473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

SQL Ignorance

I've been asked by a friend to help with his Movie database. It's an
informal home user record of his own movie watching history.
He's asked me to write a particular query, and I'm stumped.

I have two questions.

He has tblMovie and tblActor (and others.). They're Joined with
tblMovieActor, because one Movie can have many Actors and one actor can
have many Movies. He doesn't have a record for all Actors in each movie
but does just his favourites. Other Actors in the Movie will be in a block
of text in the IMDBInfo Memo field.

Here's part of his structure.

tblMovie
MovieID (PK)
Title Txt
IMDBInfo Memo
.....More Fields.

tblActor
ActorID (PK)
FullName Txt (e.g. Clint Eastwood. In his database, NameFirst and
NameLast.)
.... More Fields

The Join Table
tblMovieActor
MovieActorID (PK)
MovieID Long
ActorID Long
Character Text
------------------------

This works well for him with a Query such as this:
-------- sql ------------
SELECT
tblActor.FullName
, tblMovieActor.Character
, tblMovie.Title
, tblMovie.IMDBInfo
FROM
tblMovie
RIGHT JOIN
(tblActor
RIGHT JOIN
tblMovieActor
ON
tblActor.ActorID = tblMovieActor.ActorID)
ON
tblMovie.MovieID = tblMovieActor.MovieID;
------- /sql ------------

He can use this WHERE Clause:

---Part sql ------------
WHERE
tblActor.ActorID In (
SELECT tblMovieActor.ActorID
FROM tblMovieActor
GROUP BY tblMovieActor.ActorID
HAVING Count(tblMovieActor.ActorID)>1;)
------- /sql ------------

The whole query (which works well) is:
-------- sql ------------
SELECT
tblActor.FullName
, tblMovieActor.Character
, tblMovie.Title
, tblMovie.IMDBc
FROM
tblMovie
RIGHT JOIN
(tblActor
RIGHT JOIN
tblMovieActor
ON
tblActor.ActorID = tblMovieActor.ActorID)
ON
tblMovie.MovieID = tblMovieActor.MovieID
WHERE
tblActor.ActorID In (
SELECT
tblMovieActor.ActorID
FROM
tblMovieActor
GROUP BY
tblMovieActor.ActorID
HAVING
Count(tblMovieActor.ActorID)>1;)
ORDER BY
tblActor.FullName;
-------------------

Here's Question 1.

Obviously (?), the WHERE Clause could be applied to either
(tblActor.ActorID in the Actor Table,) or (tblMovieActor.ActorID in the
JOIN Table.)

Which (if either) is "right", or conceptually better?

The question may be either complex or trivial. I'm still learning about
types of Join and how to use them. With luck I hope to learn enough to
answer my second question myself.

Question 2
How do I write a query that ...
My explanation is going to be bad. Sorry.

Assume Clint Eastwood is ActorID 42

The tblMovie has a Memo field IMDBInfo.
This contains information about the Movie, and mentions other Actors in the
Movie.
For example, MovieID 64 named "I made this Title Up." (linked to Rowan
Atkinson ActorID 32 as an Actor in MovieID 64) may have this text: "...
Shirley Temple, Clint Eastwood, Rowan Atkinson, Fred Dagg, ..."

Here's the question.
I want the query to select each Actor and (say) in the test for Clint
Eastwood, return the MovieID 64 because Clint Eastwood is in the IMDBInfo
field.

I think if I could ask the question properly I'd be a few steps closer to
the answer.
Any clues, anyone? Including, I suspect, Clue Number One.
I can get a result by creating a new table in code using nested RecordSets,
but I'd like to know at least if a Query is possible.

Thanks,
Alan
Jun 3 '07 #1
4 1444
Alan Forsyth <No*@iHome.nzwrote in
news:Xn************************@yourdomain.com:
I've been asked by a friend to help with his Movie database.
It's an informal home user record of his own movie watching
history. He's asked me to write a particular query, and I'm
stumped.

I have two questions.
[snip]
>
Here's Question 1.

Obviously (?), the WHERE Clause could be applied to either
(tblActor.ActorID in the Actor Table,) or
(tblMovieActor.ActorID in the JOIN Table.)

Which (if either) is "right", or conceptually better?
When querying on an outer join (left or right) one puts the
criteria in the predominant table, (that's the one that should
return all records). The reason for that is that when you query
on the limited table, (the one having matching records), unless
you specifically handle the null records, the query essentially
returns the same records as an inner join

so your FROM statement
RIGHT JOIN
(tblActor
RIGHT JOIN
tblMovieActor)
implies that you should use tblMovieActor.

However, if you used proper referential integrity, you should
not have any tblMovieActor rows with no corresponding actor or
movie, making the whole point of right join moot.
>
The question may be either complex or trivial. I'm still
learning about types of Join and how to use them. With luck I
hope to learn enough to answer my second question myself.

Question 2
How do I write a query that ...
My explanation is going to be bad. Sorry.

Assume Clint Eastwood is ActorID 42

The tblMovie has a Memo field IMDBInfo.
This contains information about the Movie, and mentions other
Actors in the Movie.
For example, MovieID 64 named "I made this Title Up." (linked
to Rowan Atkinson ActorID 32 as an Actor in MovieID 64) may
have this text: "... Shirley Temple, Clint Eastwood, Rowan
Atkinson, Fred Dagg, ..."

Here's the question.
I want the query to select each Actor and (say) in the test
for Clint Eastwood, return the MovieID 64 because Clint
Eastwood is in the IMDBInfo field.

I think if I could ask the question properly I'd be a few
steps closer to the answer.
Any clues, anyone? Including, I suspect, Clue Number One.
I can get a result by creating a new table in code using
nested RecordSets, but I'd like to know at least if a Query is
possible.

Thanks,
Alan


--
Bob Quintal

PA is y I've altered my email address.

--
Posted via a free Usenet account from http://www.teranews.com

Jun 3 '07 #2
On Sat, 02 Jun 2007 23:33:50 -0500, Alan Forsyth <No*@iHome.nzwrote:

Re 1:
Better in the "primary" table, so tblActor. Putting criteria on an
outer join table can lead to unexpected results.

Re 2:
To test for occurrence of a substring in a string, you can use the
InStr function:
.... where InStr(IMDBInfo, FullName,vbTextCompare) 0

-Tom.

..
>I've been asked by a friend to help with his Movie database. It's an
informal home user record of his own movie watching history.
He's asked me to write a particular query, and I'm stumped.

I have two questions.

He has tblMovie and tblActor (and others.). They're Joined with
tblMovieActor, because one Movie can have many Actors and one actor can
have many Movies. He doesn't have a record for all Actors in each movie
but does just his favourites. Other Actors in the Movie will be in a block
of text in the IMDBInfo Memo field.

Here's part of his structure.

tblMovie
MovieID (PK)
Title Txt
IMDBInfo Memo
....More Fields.

tblActor
ActorID (PK)
FullName Txt (e.g. Clint Eastwood. In his database, NameFirst and
NameLast.)
... More Fields

The Join Table
tblMovieActor
MovieActorID (PK)
MovieID Long
ActorID Long
Character Text
------------------------

This works well for him with a Query such as this:
-------- sql ------------
SELECT
tblActor.FullName
, tblMovieActor.Character
, tblMovie.Title
, tblMovie.IMDBInfo
FROM
tblMovie
RIGHT JOIN
(tblActor
RIGHT JOIN
tblMovieActor
ON
tblActor.ActorID = tblMovieActor.ActorID)
ON
tblMovie.MovieID = tblMovieActor.MovieID;
------- /sql ------------

He can use this WHERE Clause:

---Part sql ------------
WHERE
tblActor.ActorID In (
SELECT tblMovieActor.ActorID
FROM tblMovieActor
GROUP BY tblMovieActor.ActorID
HAVING Count(tblMovieActor.ActorID)>1;)
------- /sql ------------

The whole query (which works well) is:
-------- sql ------------
SELECT
tblActor.FullName
, tblMovieActor.Character
, tblMovie.Title
, tblMovie.IMDBc
FROM
tblMovie
RIGHT JOIN
(tblActor
RIGHT JOIN
tblMovieActor
ON
tblActor.ActorID = tblMovieActor.ActorID)
ON
tblMovie.MovieID = tblMovieActor.MovieID
WHERE
tblActor.ActorID In (
SELECT
tblMovieActor.ActorID
FROM
tblMovieActor
GROUP BY
tblMovieActor.ActorID
HAVING
Count(tblMovieActor.ActorID)>1;)
ORDER BY
tblActor.FullName;
-------------------

Here's Question 1.

Obviously (?), the WHERE Clause could be applied to either
(tblActor.ActorID in the Actor Table,) or (tblMovieActor.ActorID in the
JOIN Table.)

Which (if either) is "right", or conceptually better?

The question may be either complex or trivial. I'm still learning about
types of Join and how to use them. With luck I hope to learn enough to
answer my second question myself.

Question 2
How do I write a query that ...
My explanation is going to be bad. Sorry.

Assume Clint Eastwood is ActorID 42

The tblMovie has a Memo field IMDBInfo.
This contains information about the Movie, and mentions other Actors in the
Movie.
For example, MovieID 64 named "I made this Title Up." (linked to Rowan
Atkinson ActorID 32 as an Actor in MovieID 64) may have this text: "...
Shirley Temple, Clint Eastwood, Rowan Atkinson, Fred Dagg, ..."

Here's the question.
I want the query to select each Actor and (say) in the test for Clint
Eastwood, return the MovieID 64 because Clint Eastwood is in the IMDBInfo
field.

I think if I could ask the question properly I'd be a few steps closer to
the answer.
Any clues, anyone? Including, I suspect, Clue Number One.
I can get a result by creating a new table in code using nested RecordSets,
but I'd like to know at least if a Query is possible.

Thanks,
Alan
Jun 3 '07 #3
Tom van Stiphout <no*************@cox.netwrote in
news:rf********************************@4ax.com:
On Sat, 02 Jun 2007 23:33:50 -0500, Alan Forsyth <No*@iHome.nzwrote:

Re 1:
Better in the "primary" table, so tblActor. Putting criteria on an
outer join table can lead to unexpected results.
I understand that. I was referring to a case where the decision was
purely arbitrary, in the sense that either choice could never affect the
results.
I've decided that in that case my own arbitrary choice will be to use the
Table which created the ID.
Re 2:
To test for occurrence of a substring in a string, you can use the
InStr function:
... where InStr(IMDBInfo, FullName,vbTextCompare) 0

-Tom.
That's the nudge I needed, Tom. I was focused on using the Like Operator,
but of course Instr is just what I needed.

You know of course that if you use vbTextCompare then you must specify a
starting position. An obvious Typo above. :-)
You may not know that in Access 97 the Query Wizard insists that
vbTextCompare is a Text string and surrounds it with quotes, even if I
get in first with (vbTextCompare)
A quick function worked perfectly, though.
Function ActorIsHere(strIMDBInfo, strFullName)
ActorIsHere = InStr(1, strIMDBInfo, strFullName, vbTextCompare)
End Function
Using that, I get exactly what I want by using:

SELECT tblActor.ActorID,
tblMovie.MovieID,
ActorIsHere([tblMovie].[IMDBInfo],[tblActor].[FullName])
AS theChoice
FROM tblActor, tblMovie
WHERE
(((ActorIsHere([tblMovie].[IMDBInfo],[tblActor].[FullName]))>0));

Just what I needed.

Thanks,
Alan

<snip>
Jun 4 '07 #4
Bob Quintal <rq******@sPAmpatico.cawrote in
news:Xn**********************@66.150.105.47:

[snip]
>>
Here's Question 1.

Obviously (?), the WHERE Clause could be applied to either
(tblActor.ActorID in the Actor Table,) or
(tblMovieActor.ActorID in the JOIN Table.)

Which (if either) is "right", or conceptually better?

When querying on an outer join (left or right) one puts the
criteria in the predominant table, (that's the one that should
return all records). The reason for that is that when you query
on the limited table, (the one having matching records), unless
you specifically handle the null records, the query essentially
returns the same records as an inner join

so your FROM statement
RIGHT JOIN
(tblActor
RIGHT JOIN
tblMovieActor)
implies that you should use tblMovieActor.

However, if you used proper referential integrity, you should
not have any tblMovieActor rows with no corresponding actor or
movie, making the whole point of right join moot.
And moot it is, which was the reason for my question. However, from your
information and Tom's, in situations where the choice is arbitrary my new
rule of thumb will be to choose the ID from the table where it was
created. If that choice stops making sense I'll think about it again.

In this particular situation I ended up with a Cartesian Join which
worked perfectly. I had my head stuck on criteria using "Like..."), and
Tom nudged me wiht InStr() which served me far better anyway!

Thanks,
Alan
Jun 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
5
by: John | last post by:
Hi, In a nutshell, why am I getting the results I am getting? I would have expected a call to method1( n) to return an 'n' length string. At first I thought it was a problem in my function,...
5
by: jalkadir | last post by:
Can someone please explain to me why, if I type 'Ni(ALT-164)A' at the windows prompt, I get 'Niña', but if my program does this: int main(){ std::string str("Niña"); std::cout << str <<...
2
by: Merennulli | last post by:
Ok, the project is this - I've got an upload directory on an internal server that is filled by an upload webform. This webform is working fine. So is reading in the list of files and displaying it....
13
by: Xah Lee | last post by:
Today, a motherfucker Christophe Rhodes (aka Xof in irc://chat.freenode.net/lisp ) kicked banned me. Here's the few relevant excerpt. (full, unedited excerpt will be published if there is a public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.