Overview: I want to get a list of profiles and the movies they like. All the output to be shown on one page. I am using mySQL and PHP, at moment running on localhost. Ideally i want this to be in one query, for speed.
Database table overview in mySQL as follow:
profile_tbl:
> id
> username
> about me
> gender
> hobbies
> profile_pic
> last_login
movies_tbl:
> id
> movie_name
> genre
> length
> rating
> description
profile_movies_rel_tbl
> id
> movie_id
> profile_id
Output:
I want to show 10 profiles and list of the all the movies they like. I was thinking following query:
SELECT profile_tbl.*, movies_tbl.* FROM profile_tbl
LEFT JOIN profile_movies_rel_tbl
ON profile_movies_rel_tbl.movie_id = movies_tbl.id
LEFT JOIN profile_tbl
ON profile_tbl.id= profile_movies_rel_tbl.profile_id LIMIT 10
I also have a second issue, where I want to lists all the profile that have selected movie has favorite, again the page should list profiles and movies together. In this case, i was thinking of using the above query and adding following:
WHERE profile_movies_rel_tbl.movie_id = 4
Any one need more info, please leave comment.
thanks
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `profile_movies_rel_tbl` ADD INDEX `profile_rel_idx_profile_id` (`profile_id`);
SELECT
profile_tbl.*,
movies_tbl.*
FROM
profile_tbl
LEFT JOIN
profile_movies_rel_tbl
ON profile_movies_rel_tbl.movie_id = movies_tbl.id
LEFT JOIN
profile_tbl
ON profile_tbl.id = profile_movies_rel_tbl.profile_id LIMIT 10