选择“全部”,同时在mysql

时间:2019-03-07 02:22:14

标签: php mysql mysqli

我有三个简单的表:usersprofilewatched,其中包含用户的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`;

demo on fiddle

但是我想显示用户的姓名,个人资料等。 就像在此查询中一样: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

但是它不起作用。

3 个答案:

答案 0 :(得分:1)

要获取所需的原始数据,只需将JOINusersprofile表到现有查询中,对每个用户一次:

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   email1@mail.com something about me  2   name2   email2@mail.com something about me  2           movie1,movie2
1   name1   email1@mail.com something about me  3   name3   email3@mail.com something about me  1           movie1

Demo on dbfiddle

在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";
}

答案 1 :(得分:1)

在使用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

答案 2 :(得分:1)

尝试一下

    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

输出enter image description here