[英]Select all ids from a MySql table by a user level and store the number of ids per level in an array?
[英]how to find the number of value matches with different user ids from the table
我想要使脚本显示与不同用户匹配的值的数量。 例如,在watched
表中,我拥有主要用户的id(1)和他观看的电影标题以及其他多个用户的观看电影:
+----+----------+---------+
| id | users_id | watched |
+----+----------+---------+
| 1 | 1 | movie1 |
+----+----------+---------+
| 2 | 1 | movie2 |
+----+----------+---------+
| 3 | 1 | movie3 |
+----+----------+---------+
| 4 | 2 | movie2 |
+----+----------+---------+
| 5 | 2 | movie1 |
+----+----------+---------+
| 6 | 3 | movie1 |
+----+----------+---------+
| 7 | 3 | movie5 |
+----+----------+---------+
| 8 | 3 | movie4 |
+----+----------+---------+
预期产量:
my id = `1`;
matched with id `2` 2 times on titles `movie2`, `movie1`;
matched with id `3` 1 times on titles `movie1`;
或者,如果主要用户的ID为3
:
my id = `3`;
matched with id `1` 1 times on titles `movie1`;
matched with id `2` 1 times on titles `movie1`;
等等..
我试图做类似的事情:
<?php
session_start();
$connect = mysqli_connect("127.0.0.1", "root", "root", "movie");
$query = mysqli_query($connect, "SELECT * FROM profile INNER JOIN users ON profile.users_id = users.id");
$id = $_SESSION['id'];
echo "my id: " . $id;
$movies = mysqli_query($connect, "SELECT * FROM watched WHERE users_id = '$id'");
$titles = array();
for ($i = 0;$i < $movies->num_rows;$i++)
{
$result2 = $movies->fetch_assoc();
$watched = $result2['watched'];
$find = mysqli_query($connect, "SELECT * FROM watched WHERE watched = '$watched'");
for ($l = 0;$l < $find->num_rows;$l++)
{
$found = $find->fetch_assoc();
if ($found['watched'] === $result2['watched'] and $found['users_id'] !== $id)
{
$titles[] = $found['watched'];
}
}
}
$count_titles = array();
foreach ($titles as $a)
{
@$count_titles[$a]++;
}
$matches = count($count_titles); //num of matches
for ($i = 0;$i < $query->num_rows;$i++)
{
$result = $query->fetch_assoc();
?>
<div><?php
echo "matched with id " . $result['id'] . " ";
echo $matches . " times";
foreach (array_keys($count_titles) as $key)
{
echo " on titles " . $key;
}
?></div>
<?php
} ?>
但是它仅显示整个表中的匹配总数,而不是每个用户的单独匹配总数。 我如何使其像上述那样工作?
三个表:
CREATE TABLE watched (
`id` INTEGER,
`users_id` INTEGER,
`watched` VARCHAR(6)
);
INSERT INTO watched
(`id`, `users_id`, `watched`)
VALUES
('1', '1', 'movie1'),
('2', '1', 'movie2'),
('3', '1', 'movie3'),
('4', '2', 'movie2'),
('5', '2', 'movie1'),
('6', '3', 'movie1'),
('7', '3', 'movie5'),
('8', '3', 'movie4');
CREATE TABLE users (
`id` INTEGER,
`name` VARCHAR(355),
`email` VARCHAR(355)
);
INSERT INTO users
(`id`, `name`, `email`)
VALUES
('1', 'name1', 'email1@mail.com'),
('2', 'name2', 'email2@mail.com'),
('3', 'name3', 'email3@mail.com');
CREATE TABLE profile (
`id` INTEGER,
`users_id` INTEGER,
`about` VARCHAR(355)
);
INSERT INTO profile
(`id`, `users_id`, `about`)
VALUES
('1', '1', 'something about me'),
('2', '2', 'something about me'),
('3', '3', 'something about me');
该查询将为您提供所需的原始数据:
SELECT w1.users_id AS user1, w2.users_id AS user2, 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
GROUP BY user1, user2
输出(用于样本数据):
user1 user2 movies
1 2 movie1,movie2
1 3 movie1
2 1 movie1,movie2
2 3 movie1
3 1 movie1
3 2 movie1
您可以根据需要优化查询(例如,添加WHERE w1.users_id = 1
以仅选择与用户1匹配的内容,或将JOIN
WHERE w1.users_id = 1
至users
和profile
表(每个用户ID一次)以获取用户名等)。
要获得您想要的输出,您实际上不需要更多的表,您可以使用类似以下的命令:
$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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.