mysql查询 - 统计用户之间的共同词

时间:2014-01-29 12:49:19

标签: mysql sql

我有一个非常好的查询,可以选择当前用户的朋友。示例中为user_id = 2。他的朋友是user_id = 4.

我希望使用相同的查询来获取user_id 2与所选朋友的单词数量。在这种情况下,他们有word = love,这也是他们都有的词,所以我想in_common行说= 1。

是否可以在不改变过多当前查询的情况下进行?

我应该从头开始吗?

SQL FIDDLE

1 个答案:

答案 0 :(得分:1)

假设两个用户在words_en表中都有“love”条目,那么这样的事情可能是: -

SELECT b.name_surname,
     b.avatar, 
     b.friend_words, 
     (b.friend_msg_id) AS friend_msg_id, 
     words_common.words_in_common,
     COUNT(m.id) AS unread_msg 
FROM 
(
    SELECT a.name_surname as name_surname,
        a.avatar as avatar,
        GROUP_CONCAT(DISTINCT w.word ORDER BY w.word ASC) AS friend_words,
        (a.friend_id) AS friend_msg_id
    FROM 
    (
        SELECT f1.asked_user_id AS friend_id,
            f1.created,
            u.name_surname,
            u.avatar
        FROM friends AS f1 
        INNER JOIN friends AS f2 
        ON f1.asked_user_id = f2.asker_user_id 
        AND f1.asker_user_id = f2.asked_user_id
        INNER JOIN users AS u ON f1.asked_user_id = u.id 
        WHERE f1.status = 1 AND f2.status = 1
        AND f1.asker_user_id = 2
    ) a
    LEFT JOIN connections c ON c.user_id = a.friend_id 
    AND c.invisible <> 1 AND c.deleted <> 1
    LEFT JOIN words_en w ON c.word_id = w.id 
    GROUP BY 1
) b
LEFT JOIN messages m ON m.to_user_id = 2
 AND m.from_user_id = b.friend_msg_id 
 AND m.seen = 0
LEFT OUTER JOIN 
(
    SELECT b.user_id AS friend_id, GROUP_CONCAT(a.word) AS words_in_common
    FROM words_en a
    INNER JOIN words_en b
    ON a.word = b.word
    WHERE a.user_id = 2
    GROUP BY b.user_id
) words_common
ON b.friend_msg_id = words_common.friend_id
GROUP BY b.name_surname, b.avatar, b.friend_words, b.friend_msg_id
ORDER BY unread_msg DESC

编辑 - 修改以使用连接表查找常用词: -

SELECT b.name_surname,
     b.avatar, 
     b.friend_words, 
     (b.friend_msg_id) AS friend_msg_id, 
     words_common.words_in_common,
     COUNT(m.id) AS unread_msg 
FROM 
(
    SELECT a.name_surname as name_surname,
        a.avatar as avatar,
        GROUP_CONCAT(DISTINCT w.word ORDER BY w.word ASC) AS friend_words,
        (a.friend_id) AS friend_msg_id
    FROM 
    (
        SELECT f1.asked_user_id AS friend_id,
            f1.created,
            u.name_surname,
            u.avatar
        FROM friends AS f1 
        INNER JOIN friends AS f2 
        ON f1.asked_user_id = f2.asker_user_id 
        AND f1.asker_user_id = f2.asked_user_id
        INNER JOIN users AS u ON f1.asked_user_id = u.id 
        WHERE f1.status = 1 AND f2.status = 1
        AND f1.asker_user_id = 2
    ) a
    LEFT JOIN connections c ON c.user_id = a.friend_id 
    AND c.invisible <> 1 AND c.deleted <> 1
    LEFT JOIN words_en w ON c.word_id = w.id 
    GROUP BY 1
) b
LEFT JOIN messages m ON m.to_user_id = 2
 AND m.from_user_id = b.friend_msg_id 
 AND m.seen = 0
LEFT OUTER JOIN 
(
    SELECT b.user_id AS friend_id, GROUP_CONCAT(c.word) AS words_in_common
    FROM connections a
    INNER JOIN connections b
    ON a.word_id = b.word_id
    INNER JOIN words_en c
    ON b.word_id = c.id
    WHERE a.user_id = 2
    GROUP BY b.user_id
) words_common
ON b.friend_msg_id = words_common.friend_id
GROUP BY b.name_surname, b.avatar, b.friend_words, b.friend_msg_id
ORDER BY unread_msg DESC