我有三个简单的表:users
,profile
和watched
,其中包含用户的id和他们观看的电影片名。
并且下面的脚本显示表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";
}
输出:
my id = `1`;
matched with id `2` 2 times on titles `movie2`, `movie1`;
matched with id `3` 1 times on titles `movie1`;
但我还要显示用户的姓名,个人资料等。喜欢这个查询:SELECT * FROM profile INNER JOIN users ON profile.users_id = users.id
如何合并第一个和第二个查询?
尝试:
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
但它不起作用。
试试这个
SELECT w1.users_id AS user1,u.name as name, p.*, u.*, 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
JOIN users u ON u.id=w1.users_id
JOIN profile p ON p.users_id=w1.users_id
WHERE w1.users_id =1 GROUP BY user1, user2
要获取所需的原始数据,您只需要将JOIN
users
和profile
表格分配给现有查询,每个用户一次:
SELECT w1.users_id AS id1,
u1.name AS user1,
u1.email AS user1_email,
p1.about AS user1_about,
w2.users_id AS id2,
u2.name AS user2,
u2.email AS user2_email,
p2.about AS user2_about,
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
JOIN users u1 ON u1.id = w1.users_id
JOIN profile p1 ON p1.users_id = u1.id
JOIN users u2 ON u2.id = w2.users_id
JOIN profile p2 ON p2.users_id = u2.id
WHERE w1.users_id = 1
GROUP BY id1, user1, user1_email, user1_about, id2, user2, user2_email, user2_about
输出:
id1 user1 user1_email user1_about id2 user2 user2_email user2_about num_movies movies
1 name1 [email protected] something about me 2 name2 [email protected] something about me 2 movie1,movie2
1 name1 [email protected] something about me 3 name3 [email protected] something about me 1 movie1
在PHP中,您可能会执行以下操作:
$id = $_SESSION['id'];
$first = true;
$movies = mysqli_query($connect, "... the query above ...");
while ($row = $movies->fetch_assoc()) {
if ($first) {
echo "User {$row['user1']} ({$row['user1_email']}, {$row['user1_about']}):\n";
$first = false;
}
echo "matched with {$row['user2']} ({$row['user2_email']}) {$row['num_movies']} times on titles {$row['movies']}\n";
}
在连接之后使用profile
将连接与watched
放在一起,获取其他用户的ID。
SELECT p.*, w2.watched,
COUNT(*) AS num_movies,
GROUP_CONCAT(w2.watched order by w2.watched) as movies
FROM watched AS w1
JOIN watched w2 ON w2.watched = w1.watched AND w2.users_id != w1.users_id
JOIN users AS u ON u.id = w2.users_id
JOIN profile AS p ON p.users_id = u.id
WHERE w1.users_id = $id
GROUP BY w2.users_id