1

我需要 MYSQL 搜索查询来从我的表中获取热门话题,下面是我需要的解释

+----+---------+-----------------------------+
| ID | ID_user | text                        | 
+----+---------+-----------------------------+
| 1  | bruno   | michael jackson is dead     |
| 2  | thomasi | michael j. moonwalk is dead |
| 3  | userts  | michael jackson lives       |
+----+---------+-----------------------------+

我想查询表中重复次数最多的单词,限制前10,结果可能是这样的:

+-------+------------+
| count | word       |
+-------+------------+
| 3     | michael    |
| 2     | dead       |
| 2     | jackson    |
| 1     | j.         |
| 1     | lives      |
| 1     | moonwalk   |
+-------+------------+

但我只想搜索重复超过 10 次的单词,在这种情况下没有出现任何单词,但如果重复单词的条件为 2,它将只显示“michael”和“dead”,但忽略“is”,因为我没有想要长度少于 2 个字符的单词,以及一个短语的单词,那么我需要出现这个:

+-------+-----------------+
| count | word            |
+-------+-----------------+
| 2     | michael jackson |
| 2     | dead            |
+-------+-----------------+
4

1 回答 1

1
CREATE TEMPORARY TABLE counters (id INT);
-- insert into counters as much as you like (values here means "number of repeats"
INSERT INTO counters VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9),(10),
                           (11),(12),(13),(14),(15),(16),(17),(18),(19),(20),
                           (21),(22),(23),(24),(25),(26),(27),(28),(29),(30);

  SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(texts.text,' ',counters.id),' ',-1) AS word,
         COUNT(counters.id) AS counter
    FROM texts
         INNER JOIN counters ON (LENGTH(text)>0 AND SUBSTRING_INDEX(SUBSTRING_INDEX(text,' ',counters.id),' ',-1) <> SUBSTRING_INDEX(SUBSTRING_INDEX(text,' ',counters.id-1),' ', -1))
   WHERE length(SUBSTRING_INDEX(SUBSTRING_INDEX(texts.text,' ',counters.id),' ',-1)) > 2
GROUP BY word
  HAVING COUNT(counters.id) > 1
ORDER BY counter desc;

但这不是很有效,不应该那样做

编辑:

  SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(texts.text,' ',counters.id),' ',-1) AS word,
         COUNT(counters.id) AS counter
    FROM texts
         INNER JOIN counters ON (LENGTH(text)>0 AND SUBSTRING_INDEX(SUBSTRING_INDEX(text,' ',counters.id),' ',-1) <> SUBSTRING_INDEX(SUBSTRING_INDEX(text,' ',counters.id-1),' ', -1))
   -- exclude words list
   WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(texts.text,' ',counters.id),' ',-1) NOT IN ('is', 'of', 'this', 'to')
GROUP BY word
  HAVING COUNT(counters.id) > 1
ORDER BY counter desc;
于 2013-05-28T22:47:55.067 回答