Find all movies with any actor in common

Posted on 2007-07-22
Last Modified: 2013-12-20
 I have an Access 2003 DB in support of a VB6 application.  The Access DB has two tables (named movies and ActorsLink) which are joined on the common field 'MovieID'.  The ActorsLink table contains every ActorID for all MovieID's in the DB.  (i.e. If 15 Actors appear in one movie, each of the 15 has a separate row along with the MovieID in question.)
  I'm looking for the correct syntax for a parameter query.  If a MovieID is entered, I need the query to return all other MovieID's and Titles which HAVE AT LEAST ONE ACTOR IN COMMON with the MovieID entered.
Does anyone know how to do this?
Question by:wjshore
    LVL 84
    This would return all movie titles where the MovieID = 1 and ActorID=2

    SELECT Title FROM Movies INNER JOIN ActorsLink ON Movies.MovieID=ActorsLink.MovieID WHERE ActorsLink.MovieID=1 AND ActorsLink.ActorID=2

    I'm making assumptions of your Table and Column names; you'll need to change them to match those in your project. In general, I've found it easiest to use the Query designer in Access to build your query, then you can just copy/paste the SQL into your VB project to build a recordset or whatever ...

    The syntax for an Inner Join query is like this:

    SELECT field1, field2, field3
    FROM first_table
    INNER JOIN second_table
    ON first_table.keyfield = second_table.foreign_keyfield

    You can also use Right or Left joins (which return all records from the "right" or "left" table, with the Right generally being the first table specified).

    See here for more syntax on Joins:
    LVL 77

    Expert Comment

    SELECT M.MovieidID, M.Movietitle
    FROM Movies
    inner join
    (SELECT DISTINCT Actorlink.MovieID
    FROM Actorlink INNER JOIN
    (SELECT Actorlink.*
    FROM Actorlink
    WHERE Actorlink.Movieid=[enterid]) as q2
    ON Actorlink.ActorID = Q2.ActorIdID) as q1
    on Movies.Movieid = q1.Movieid
    LVL 58

    Accepted Solution

    Yes, this needs the table ActorsLink twice: once for the movies-to-actors links, once for the actors-to-movies link... As a parameter query, it could be:

    PARAMETERS [Enter Movie ID:] Long;
    FROM (
        Movies AS M
        INNER JOIN ActorsLink AS MA ON M.MovieID = MA.MovieID )
        INNER JOIN ActorsLink AS AM ON MA.ActorID = AM.ActorID
    WHERE AM.MovieID=[Enter Movie ID:] AND M.MovieID<>[Enter Movie ID:];

    PARAMETERS creates the parameter formally
    SELECT DISTINCTROW is essential to show each movie only once
    FROM uses ActorsLink twice (MA and AM)
    WHERE selects the desired movies, but not itself.

    Good luck!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
    I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
    Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
    In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now