I have three simple tables: users
, profile
and watched
, which contains users' id and their watched movie titles.
and the script below displays the matched values with different users from the table watched
:
$id = $_SESSION['id'];
echo "my id: $id\n";
$movies = mysqli_query($connect, "SELECT w1.users_id AS user1,
w2.users_id AS user2,
COUNT(w2.watched) AS num_movies,
GROUP_CONCAT(w2.watched ORDER BY w2.watched) AS movies
FROM watched w1
JOIN watched w2 ON w2.watched = w1.watched AND w2.users_id != w1.users_id
WHERE w1.users_id = $id
GROUP BY user1, user2");
while ($row = $movies->fetch_assoc()) {
echo "matched with id {$row['user2']} {$row['num_movies']} times on titles {$row['movies']}\n";
}
output:
my id = `1`;
matched with id `2` 2 times on titles `movie2`, `movie1`;
matched with id `3` 1 times on titles `movie1`;
BUT I want to also display the users' names, profiles and so on.
like in this query: SELECT * FROM profile INNER JOIN users ON profile.users_id = users.id
how can I merge the first and the second queries?
tried:
SELECT *, w2.watched,
COUNT(w2.watched) AS num_movies,
GROUP_CONCAT(w2.watched order by w2.watched) as movies
FROM profile AS p
JOIN users AS u ON p.users_id = u.id
LEFT JOIN watched AS w ON w.users_id = u.id and w.watched=w2.watched
WHERE u.id != $id
GROUP BY w.users_id
but it's not working.