作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有三个简单的表: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
关于mysql - 选择 "all"同时在mysql中选择 "individually",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55035072/
我是一名优秀的程序员,十分优秀!