SQL/Database Help |
Author: |
Message: |
djdannyp
Elite Member
Danny <3 Sarah
Posts: 3546 Reputation: 31
38 / /
Joined: Mar 2006
|
O.P. SQL/Database Help
Okay, I have a database of films.....I want a search function to be able to find any films that a given pair of actors appear in (Owen Wilson & Ben Stiller, for example) when they could be in any one of the 4 "Actor" fields.
Here's the code I have:
sql code: SELECT tblFilms.ID, tblFilms.Title, tblFilms.Year, tblFilms.Actor1, tblFilms.Actor2, tblFilms.Actor3, tblFilms.Actor4
FROM tblFilms
WHERE (((tblFilms.Actor1)=[Enter the first actor who's films you would like to find] Or (tblFilms.Actor1)=[Enter the second actor who's films you would like to find])) OR (((tblFilms.Actor2)=[Enter the first actor who's films you would like to find] Or (tblFilms.Actor2)=[Enter the second actor who's films you would like to find])) OR (((tblFilms.Actor3)=[Enter the first actor who's films you would like to find] Or (tblFilms.Actor3)=[Enter the second actor who's films you would like to find])) OR (((tblFilms.Actor4)=[Enter the first actor who's films you would like to find] Or (tblFilms.Actor4)=[Enter the second actor who's films you would like to find]));
That's the closest I could get.....but as you'll know, that finds any film which EITHER actor was in. My SQL knowledge isn't good enough to figure out how to get it to work properly, but I'm sure it'll be easy enough for someone who knows what they're doing!
Thanks
|
|
03-18-2010 06:42 PM |
|
|
Mnjul
forum super mod
plz wub me
Posts: 5396 Reputation: 58
– / /
Joined: Nov 2002
Status: Away
|
RE: SQL/Database Help
The best I can come up with is:
sql code: SELECT those_columns FROM tblFilms WHERE (tblFilms.Actor1=NAME1 OR tblFilms.Actor2=NAME1 OR tblFilms.Actor3=NAME1 OR tblFilms.Actor4=NAME1)
INTERSECT
SELECT those_columns FROM tblFilms WHERE (tblFilms.Actor1=NAME2 OR tblFilms.Actor2=NAME2 OR tblFilms.Actor3=NAME2 OR tblFilms.Actor4=NAME2)
But I dunno if your SQL engine supports INTERSECT (at least MySQL doesn't seem so). But if you're just doing an assignment then I think it's fine.
This post was edited on 03-18-2010 at 06:53 PM by Mnjul.
|
|
03-18-2010 06:52 PM |
|
|
djdannyp
Elite Member
Danny <3 Sarah
Posts: 3546 Reputation: 31
38 / /
Joined: Mar 2006
|
O.P. RE: SQL/Database Help
I'm using Microsoft Access 2007....when I try using INTERSECT it says there's a syntax error and missing operation
|
|
03-18-2010 07:02 PM |
|
|
Mnjul
forum super mod
plz wub me
Posts: 5396 Reputation: 58
– / /
Joined: Nov 2002
Status: Away
|
RE: SQL/Database Help
Well, it appears that it's not supported on Access
Anyway, when I googled a lot people say INTERSECT can be implemented using INNER JOIN, but I'm not sure how it works exactly. You'll have to fiddle with it.
|
|
03-18-2010 07:05 PM |
|
|
djdannyp
Elite Member
Danny <3 Sarah
Posts: 3546 Reputation: 31
38 / /
Joined: Mar 2006
|
O.P. RE: SQL/Database Help
It's a Report that takes its info from the query
|
|
03-18-2010 07:18 PM |
|
|
toddy
Veteran Member
kcus uoy
Posts: 2573 Reputation: 49
– / /
Joined: Jun 2004
|
RE: SQL/Database Help
edit:
maybe not
This post was edited on 03-18-2010 at 08:19 PM by toddy.
|
|
03-18-2010 07:42 PM |
|
|
djdannyp
Elite Member
Danny <3 Sarah
Posts: 3546 Reputation: 31
38 / /
Joined: Mar 2006
|
|
03-18-2010 07:56 PM |
|
|
matty
Scripting Guru
Posts: 8336 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
RE: SQL/Database Help
You don't want to use an INNER JOIN in this case as an INNER JOIN will join 2 tables with like values.
sql code: SELECT * FROM tblFilms
WHERE (Actor1=NAME1 OR Actor2=NAME1 OR Actor3=NAME1 OR Actor4=NAME1)
AND (Actor1=NAME2 OR Actor2=NAME2 OR Actor3=NAME2 OR Actor4=NAME2)
toddy yours wouldn't work because you are checking if tblFilms.Actor1 = Name1 AND tblFilms.Actor2 = Name1 AND etc.....
This post was edited on 03-18-2010 at 08:08 PM by matty.
|
|
03-18-2010 08:06 PM |
|
|
djdannyp
Elite Member
Danny <3 Sarah
Posts: 3546 Reputation: 31
38 / /
Joined: Mar 2006
|
O.P. RE: SQL/Database Help
We have a winner.....thanks Matty
|
|
03-18-2010 08:09 PM |
|
|
matty
Scripting Guru
Posts: 8336 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
RE: SQL/Database Help
No problem djdannyp.
Here is an example
Attachment: example.jpg (117.16 KB)
This file has been downloaded 242 time(s).
This post was edited on 03-18-2010 at 08:18 PM by matty.
|
|
03-18-2010 08:17 PM |
|
|
Pages: (3):
« First
[ 1 ]
2
3
»
Last »
|
|