从两个表中查找唯一ID并从两个表中查找相应的计数

时间:2017-05-31 07:47:45

标签: mysql sql join

我正在运行 mysql 数据库。我有两个表分别在帖子上存储喜欢和评论。

喜欢表:

+--------------+------------------+------+-----+---------+----------------+
| Field        | Type             | Null | Key | Default | Extra          |
+--------------+------------------+------+-----+---------+----------------+
| id           | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| post_id      | int(11)          | NO   |     | NULL    |                |
| user_id      | varchar(255)     | NO   |     | NULL    |                |
| created_at   | timestamp        | YES  |     | NULL    |                |
| updated_at   | timestamp        | YES  |     | NULL    |                |
+--------------+------------------+------+-----+---------+----------------+

评论表:

+--------------+------------------+------+-----+---------+----------------+
| Field        | Type             | Null | Key | Default | Extra          |
+--------------+------------------+------+-----+---------+----------------+
| id           | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| post_id      | int(11)          | NO   |     | NULL    |                |
| user_id      | varchar(255)     | NO   |     | NULL    |                |
| comment      | varchar(255)     | NO   |     | NULL    |                |
| created_at   | timestamp        | YES  |     | NULL    |                |
| updated_at   | timestamp        | YES  |     | NULL    |                |
+--------------+------------------+------+-----+---------+----------------+

我需要获得唯一的用户ID及其喜欢和评论计数。

我运行了以下查询:

select * from
(select user_id, count(*) as like_count from post_likes group by user_id) as a,
(select user_id, count(*) as comment_count from post_comments group by user_id) as b
limit 5

查询在180万条记录上花费了66秒(两个表合并在一起)。而且,它没有给出正确的结果。

如果我执行LEFT JOIN,它只返回左表中的匹配记录,而我需要两者,即如果用户ID只是喜欢而没有评论或只评论和不喜欢。

我想要的是:

+--------------+------------------+------+-----+---------+
| user_id      | like_count       | comment_count        |
+--------------+------------------+------+-----+---------+
| 1            | 1213             | 310                  |
| 2            | 1098             | 0                    |
| 3            | 0                | 115                  |
+--------------+------------------+------+-----+---------+

3 个答案:

答案 0 :(得分:3)

理想的方法是在用户表上LEFT JOIN,例如:

SELECT u.user_id, COUNT(l.id), COUNT(c.id)
FROM users u LEFT JOIN Likes l ON u.user_id = l.user_id
LEFT JOIN Comments c ON u.user_id = c.user_id;

答案 1 :(得分:2)

改为UNION ALL,然后合并行:

select user_id, sum(like_count), sum(comment_count)
from
(
    select user_id, count(*) as like_count, null as comment_count
    from post_likes
    group by user_id
    union all
    select user_id, null, count(*)
    from post_comments
    group by user_id
) as dt
group by user_id

答案 2 :(得分:0)

一种方法是使用UNION ALL条件聚合

SELECT user_id,
       SUM(type='likes') AS like_count,
       SUM(type='comments') AS comment_count
FROM (
   SELECT user_id, 'likes' AS type
   FROM post_likes 

   UNION ALL

   SELECT user_id,  'comments' AS type
   FROM post_comments) AS t
GROUP BY user_id

此查询不会返回没有喜欢也没有评论的用户。