I have tree tables. videos, categories and video_categories (which is basically a pivot table between videos and categories).
I am trying to get all the videos that do not have certain categories. The problem is that my result is containing the same video more than once, and i only need it once.
This is what i got
SELECT videos.* FROM videos
JOIN video_categories ON videos.id = video_categories.video_id
WHERE category_id NOT IN (64452, 1031, 32595, 1015, 26484, 1019)
ORDER BY downloads
And the result i get is containing one video the same number of times as the videos number of categories. Also one more thing is that videos table has 55 300 rows, and pivot table video_categories has 400 000 rows, so i cant just use DISTINCT as than query runs for about 10 seconds.
I am calling this query from PHP, and excluding categories manually from retrieved is not possible because of pagination.
Any suggestions how can i do this?
EDIT One video can have many categories. So it is many to many relation.