I'm trying to do something very simple in CF/SQL but just can't seem to figure out what I am doing wrong.
I have these tables:
movies genres actors moviesGenres moviesActors
------ ------ ------ ------------ ------------
movieId genreId actorId id id
title name fname movie movie
lname genre actor
I'm trying to write a query that simply lists all movies, with each movie's genre (multiple selections possible) and each movie's actors (again, multiple selections possible).
When I write the query to include just the genres, all works fine. I use for the query itself, grouping by movieId, and then a separate around the genres.
But when I try to then include the actors, everything blows up and it seems as if the grouping breaks down.
Here's the SQL query for both joins:
SELECT m.movieId, m.title, m.releaseDate, m.description, g.name, a.fname, a.lname
FROM movies m
INNER JOIN genres g ON g.genreId IN (SELECT genre FROM moviesGenres WHERE movie = m.movieId)
INNER JOIN actors a ON a.actorID IN (SELECT actor FROM moviesActors WHERE movie = m.movieId)
ORDER BY m.title
Thanks in advance for any help!
UPDATE:
The query supplied by Leigh and Mark seems to work overall, but I am still seeing the actors displayed multiple times in the . Here is my code:
<tbody>
<cfoutput query="variables.movieList" group="movieId">
<tr>
<td><a href="##">#title#</a></td>
<td><cfoutput group="name">#name# | </cfoutput></td>
<td><cfoutput group="actorId">#actorId# | </cfoutput></td>
</tr>
</cfoutput>
</tbody>
I've also tried it without grouping the final tag but that didn't work. Note that I changed the a.lName and a.fName to a.actorId for the sake of simplicity in testing.
A sample row looks like this:
The Godfather Action | Drama | 1 | 2 | 1 | 2 |