
Originally Posted by
pata
Any pointers?
let's use a portion of your database to illustrate
one movie can have multiple actors, and one movie can have multiple writers
try to combine these relationships in a single query, and boom, you get cross join effects
one thing you could do is collapse one of these relationships in a subquery, so that the subquery produces a single row per movie, with GROUP_CONCAT to collapse multiple values to one (concatenated) value
Code:
SELECT dvdpedia.id
, dvdpedia.title
, rated.rated
, w.writers
, actors.name
, actors.lastName
FROM dvdpedia
INNER
JOIN actor2title
on actor2title.titleId = dvdpedia.id
INNER
JOIN actors
on actors.id = actor2title.pId
INNER
JOIN rated
on rated.id = dvdpedia.ratedId
INNER
JOIN ( SELECT writer2dvd.titleId
, GROUP_CONCAT(CONCAT(writers.name
, ' '
, writers.lastName) AS writers
FROM writer2dvd
INNER
JOIN writers
ON writers.id = writer2dvd.pId
GROUP
BY writer2dvd.titleId ) AS w
ON w.titleId = dvdpedia.id
thus the outer query has only one one-to-many relationship, along with the one-to-one relationship with the subquery
if you also wanted to collapse the actors into a concatenated value, you would do that in a subquery as well
Bookmarks