Display a favorited movie once
Use "GROUP BY rating.tmdb_id" in the SQL to only display a film once.
Hi there! I posted this over at Stack Overflow but haven't gotten any answers so I figured I'd post here:
I'm developing a Movie tracking app in Laravel and I'm currently stuck trying to join Ratings on Movies, although I think I'm almost there I just don't know how to get exactly to where I want to be.
Here's my current working query (using ->toSql() but it's an eloquent query in my app):
SELECT
`movies`.*,
`ratings`.`user`,
`ratings`.`favorite`,
`ratings`.`created_at` AS `rating_created_at`,
`ratings`.`updated_at` AS `rating_updated_at`,
`users`.`id` AS `user_id`,
`users`.`name` AS `user_name`
FROM
`movies`
INNER JOIN
`ratings` ON `movies`.`tmdb_id` = `ratings`.`tmdb_id`
INNER JOIN
`users` ON `ratings`.`user` = `users`.`id`
WHERE
`ratings`.`favorite` = 1
GROUP BY `ratings`.`tmdb_id`
ORDER BY `ratings`.`created_at` DESC
Here's the problem with it:
Its output displays duplicate movies for each movie that has a matching ratings row since multiple people can favorite a movie: ratings.user.
What I'd like:
ratings.user column and display their name with the movieWhere I'm confused is how to be able to grab all of the user data associated with a given movie without duplicating its return. Do I need a pivot table to accomplish this?
Edit: I updated my SQL query to include the users table so I can access their names, but specifically bullet point 3 above is still an issue
Please or to participate in this conversation.