mysql - 在大型 MySQL 数据库中查找最常见的单词

标签 mysql

我有一个旧的 MySQL 数据库,其中单列包含超过 4,000,000 行。每一行都包含一个公司名称,当然,其中许多名称包含多个单词。

我想找出整个数据库中最常用的 1000 个单词。这可以使用 MySQL 查询吗?

最佳答案

考虑以下示例...

CREATE TABLE soliloquy
(line_number INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,line VARCHAR(255) NOT NULL
);

INSERT INTO soliloquy (line) VALUES
("To be, or not to be, that is the question:"),
("Whether 'tis nobler in the mind to suffer"),
("The slings and arrows of outrageous fortune,"),
("Or to take Arms against a Sea of troubles,"),
("And by opposing end them: to die, to sleep"),
("No more; and by a sleep, to say we end"),
("the heart-ache, and the thousand natural shocks"),
("that Flesh is heir to? 'Tis a consummation"),
("devoutly to be wished. To die, to sleep,"),
("To sleep, perchance to Dream; aye, there's the rub,"),
("for in that sleep of death, what dreams may come,"),
("when we have shuffled off this mortal coil,"),
("must give us pause. There's the respect"),
("that makes Calamity of so long life:"),
("For who would bear the Whips and Scorns of time,"),
("the Oppressor's wrong, the proud man's Contumely,"),
("the pangs of despised Love, the Law’s delay,"),
("the insolence of Office, and the spurns"),
("that patient merit of the unworthy takes,"),
("when he himself might his Quietus make"),
("with a bare Bodkin? Who would Fardels bear,"),
("to grunt and sweat under a weary life,"),
("but that the dread of something after death,"),
("the undiscovered country, from whose bourn"),
("no traveller returns, puzzles the will,"),
("and makes us rather bear those ills we have,"),
("than fly to others that we know not of."),
("Thus conscience does make cowards of us all,"),
("and thus the native hue of Resolution"),
("Is sicklied o'er, with the pale cast of Thought,"),
("And enterprises of great pitch and moment,"),
("with this regard their Currents turn awry,"),
("And lose the name of Action. Soft you now,"),
("The fair Ophelia? Nymph, in thy Orisons"),
("Be all my sins remember'd");

以下是每个单词出现的行数 - 与每个单词出现的频率略有不同......

哦,ints 只是一个整数表[i] (0-9)...

SELECT word
     , COUNT(*) total
  FROM 
     ( SELECT DISTINCT line_number
                     , SUBSTRING_INDEX(SUBSTRING_INDEX(line,' ',i+1),' ',-1) word
                  FROM soliloquy
                     , ints 
     ) x
 GROUP
    BY word
HAVING COUNT(*) > 3 
 ORDER
    BY total DESC
     , word;

+------+-------+
| word | total |
+------+-------+
| the  |    18 |
| of   |    14 |
| and  |    11 |
| To   |     9 |
| that |     7 |
| a    |     5 |
| we   |     4 |
+------+-------+

哦,标点符号方面可能存在一些轻微错误 - 但您应该明白。

关于mysql - 在大型 MySQL 数据库中查找最常见的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44026794/

相关文章:

mysql - memcached数据会在内存中停留多久

mysql使用inner join和max()从两个表插入一个表

android - 如何让用户(即他的位置)在谷歌地图中移动,其中用户的更新位置是从 MySql 获取的?

php - Mysql/PHP 引用问题!

php - 如何在 mysql 中更改 one day plus 的时间?

php - MYSQL WITH LIKE, OR, AND, 给出所有结果

MySql 查询掩码表

php - Laravel 如果数据库表为空

mysql - 执行从 MySql 到 Mongodb 的查询

php - 如何在分解字符串中按名字 (SQL) 排序