如何在MySQL中找到最流行的单词出现?

时间:2015-09-24 12:20:53

标签: mysql sql denormalization

我有一个名为results的表,有5列。

我想使用title列查找说:WHERE title like '%for sale%'的行,然后列出该列中最常用的字词。一个是for,另一个是sale,但我想看看其他单词与此相关。

示例数据:

title
cheap cars for sale
house for sale
cats and dogs for sale
iphones and androids for sale
cheap phones for sale
house furniture for sale

结果(单个单词):

for    6
sale    6
cheap    2
and    2
house    2
furniture 1
cars    1
etc...

6 个答案:

答案 0 :(得分:7)

您可以通过一些字符串操作来提取单词。假设您有一个数字表,并且单词由单个空格分隔:

select substring_index(substring_index(r.title, ' ', n.n), ' ', -1) as word,
       count(*)
from results r join
     numbers n
     on n.n <= length(title) - length(replace(title, ' ', '')) + 1
group by word;

如果您没有数字表,可以使用子查询手动构建一个:

from results r join
     (select 1 as n union all select 2 union all select 3 union all . . .
     ) n
     . . .

SQL Fiddle(由@GrzegorzAdamKowalski提供)是here

答案 1 :(得分:4)

您可以以一些有趣的方式使用ExtractValue。请参阅SQL小提琴:http://sqlfiddle.com/#!9/0b0a0/45

我们只需要一张桌子:

CREATE TABLE text (`title` varchar(29));

INSERT INTO text (`title`)
VALUES
    ('cheap cars for sale'),
    ('house for sale'),
    ('cats and dogs for sale'),
    ('iphones and androids for sale'),
    ('cheap phones for sale'),
    ('house furniture for sale')
;

现在我们构建一系列选择,从转换为XML的文本中提取整个单词。每个选择从文本中提取第N个单词。

select words.word, count(*) as `count` from
(select ExtractValue(CONCAT('<w>', REPLACE(title, ' ', '</w><w>'), '</w>'), '//w[1]') as word from `text`
union all
select ExtractValue(CONCAT('<w>', REPLACE(title, ' ', '</w><w>'), '</w>'), '//w[2]') from `text`
union all
select ExtractValue(CONCAT('<w>', REPLACE(title, ' ', '</w><w>'), '</w>'), '//w[3]') from `text`
union all
select ExtractValue(CONCAT('<w>', REPLACE(title, ' ', '</w><w>'), '</w>'), '//w[4]') from `text`
union all
select ExtractValue(CONCAT('<w>', REPLACE(title, ' ', '</w><w>'), '</w>'), '//w[5]') from `text`) as words
where length(words.word) > 0
group by words.word
order by `count` desc, words.word asc

答案 2 :(得分:2)

这会给你一个单词(只要我理解你的single word意味着什么。):

select concat(val,' ',cnt) as result from(
    select (substring_index(substring_index(t.title, ' ', n.n), ' ', -1)) val,count(*) as cnt
        from result t cross join(
         select a.n + b.n * 10 + 1 n
         from 
                (select 0 as n union all select 1 union all select 2 union all select 3 
                        union all select 4 union all select 5 union all select 6 
                        union all select 7 union all select 8 union all select 9) a,
                (select 0 as n union all select 1 union all select 2 union all select 3 
                        union all select 4 union all select 5 union all select 6 
                        union all select 7 union all select 8 union all select 9) b
                order by n 
        ) n
    where n.n <= 1 + (length(t.title) - length(replace(t.title, ' ', '')))
    group by val
    order by cnt desc
) as x

结果应如下所示:

Result
--------
for 6
sale 6
house 2
and 2
cheap 2
phones 1
iphones 1
dogs 1
furniture 1
cars 1
androids 1
cats 1

但如果您需要的single word是这样的话:

result
-----------
for 6 sale 6 house 2 and 2 cheap 2 phones 1 iphones 1 dogs 1 furniture 1 cars 1 androids 1 cats 1

只需将上面的查询修改为:

select group_concat(concat(val,' ',cnt) separator ' ') as result from( ...

答案 3 :(得分:0)

更新

来自https://stackoverflow.com/a/17942691/98491

的想法

此查询适用于我的机器(MySQL 5.7),但Sqlfiddle报告错误。 基本的想法是你要么在你的领域创建一个从1到最大单词出现的数字表(如4),或者像我一样,为简单起见使用UNION 1 .. 4。

CREATE TABLE products (
  `id` int,
  `name` varchar(45)
);

INSERT INTO products
    (`id`, `name`)
VALUES
    (1, 'for sale'),
    (2, 'for me'),
    (3, 'for you'),
    (4, 'you and me')
;

SELECT name, COUNT(*) as count FROM
(
SELECT
  product.id,
  SUBSTRING_INDEX(SUBSTRING_INDEX(product.name, ' ', numbers.n), ' ', -1) name
FROM
  (
    SELECT 1 AS n
    UNION SELECT 2
    UNION SELECT 3
    UNION SELECT 4
  ) AS numbers
  INNER JOIN products product
  ON CHAR_LENGTH(product.name)
     -CHAR_LENGTH(REPLACE(product.name, ' ', ''))>=numbers.n-1
ORDER BY
  id, n
)
AS result
GROUP BY name
ORDER BY count DESC

结果将是

for | 3
you | 2
me  | 2
and | 1
sale| 1

答案 4 :(得分:0)

SQL不适合这项任务,虽然可能存在限制(例如单词数)

执行相同任务的快速PHP脚本可能更容易长期使用(也可能更快)

<?php
$rows = [
    "cheap cars for sale",
    "house for sale",
    "cats and dogs for sale",
    "iphones and androids for sale",
    "cheap phones for sale",
    "house furniture for sale",
];

//rows here should be replaced by the SQL result
$wordTotals = [];
foreach ($rows as $row) {
   $words = explode(" ", $row);
    foreach ($words as $word) {
        if (isset($wordTotals[$word])) {
            $wordTotals[$word]++; 
            continue;
        }

        $wordTotals[$word] = 1;
    }
}

arsort($wordTotals);

foreach($wordTotals as $word => $count) {
    echo $word . " " . $count . PHP_EOL;
}

输出

for 6
sale 6
and 2
cheap 2
house 2
phones 1
androids 1
furniture 1
cats 1
cars 1
dogs 1
iphones 1

答案 5 :(得分:0)

这是一个有效的SQL小提琴:http://sqlfiddle.com/#!9/0b0a0/32

让我们从两个表开始 - 一个用于文本,一个用于数字:

CREATE TABLE text (`title` varchar(29));

INSERT INTO text
    (`title`)
VALUES
    ('cheap cars for sale'),
    ('house for sale'),
    ('cats and dogs for sale'),
    ('iphones and androids for sale'),
    ('cheap phones for sale'),
    ('house furniture for sale')
;

CREATE TABLE iterator (`index` int);

INSERT INTO iterator
    (`index`)
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)
;

第二个表iterator必须包含从1到N的数字,其中N高于或等于text中最长字符串的长度。

然后,运行此查询:

select
  words.word, count(*) as `count`
from 
(select
  substring(concat(' ', t.title, ' '), i.index+1, j.index-i.index) as word
from
  text as t, iterator as i, iterator as j
where
    substring(concat(' ', t.title), i.index, 1) = ' '
and substring(concat(t.title, ' '), j.index, 1) = ' '
and i.index < j.index
) AS words
where
    length(words.word) > 0
and words.word not like '% %'
group by words.word
order by `count` desc, words.word asc

有两个选择。外部单词只对单个单词进行分组和计数(长度大于0且没有任何空格的单词)。内部字符串从任何空格字符开始提取所有字符串,并以任何其他空格字符结尾,因此字符串不是单词(尽管命名此子查询words),因为它们可以包含除开始和结束之外的其他空格。 / p>

结果:

word    count
for     6
sale    6
and     2
cheap   2
house   2
androids    1
cars    1
cats    1
dogs    1
furniture   1
iphones     1
phones  1