我有一个存储用户兴趣的数据库表:
CREATE TABLE `ade39_findme_settings_words` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`word` varchar(255) DEFAULT NULL,
`language` varchar(2) DEFAULT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `clickeditem` (`user_id`),
KEY `count` (`word`)
) ENGINE=MyISAM AUTO_INCREMENT=134 DEFAULT CHARSET=utf8;
word = interest
现在我想在此表中搜索具有一个或多个相同兴趣的用户,并按相同兴趣的数量对此列表进行排序。知道sql查询的样子吗?
喜欢搜索“具有与user_id = 62相似(!)兴趣的所有用户”
示例:
USER INTEREST
62 riding
62 reading
62 dancing
75 riding
75 dancing
80 riding
81 rid
输出:
USER INTEREST SAME_COUNT
75 riding 2
80 riding 1
81 rid 1
感谢。
答案 0 :(得分:0)
尝试此查询。
SELECT user AS USER ,interest AS INTEREST,Count(*)AS SAME_COUNT
FROM ade39_findme_settings_words WHERE interest like '%rid%'
GROUP BY interest ORDER BY COUNT(*) DESC;
它给你所有的兴趣,在你的情况下有75个和80个用户有两个时间,但在我的查询组兴趣,输出根据匹配兴趣关键字给出1。 希望这将完成您的要求。
答案 1 :(得分:0)
我想在此表中搜索具有一个或多个相同的用户 利益
这个要求有点模糊,但是我创建了一个示例查询,以获得除用户以外与用户62具有相同兴趣的所有用户:
mysql> SELECT user_id,word FROM ade39_findme_settings_words;
+---------+---------+
| user_id | word |
+---------+---------+
| 62 | riding |
| 62 | reading |
| 62 | dancing |
| 75 | riding |
| 75 | dancing |
| 80 | riding |
| 81 | rid |
+---------+---------+
mysql> SELECT
word,
GROUP_CONCAT(user_id) AS users,
COUNT(*) as cnt
FROM
ade39_findme_settings_words
WHERE
user_id != 62
AND word IN (
SELECT word
FROM ade39_findme_settings_words
WHERE user_id=62
)
GROUP BY word
ORDER BY cnt DESC;
+---------+-------+-----+
| word | users | cnt |
+---------+-------+-----+
| riding | 75,80 | 2 |
| dancing | 75 | 1 |
+---------+-------+-----+
2 rows in set (0.00 sec)
第二个查询有一个子查询,它返回用户62的所有兴趣。