我有一个名为results5列的表格。
results
我想使用该title列查找要说的行:WHERE title like '%for sale%'然后在该列中列出最受欢迎的单词。一个会是for,另一个会是,sale但是我想看看与此相关的其他词。
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...
您可以通过一些字符串操作来提取单词。假设您有一个数字表,并且单词之间用单个空格分隔:
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小提琴(由@GrzegorzAdamKowalski提供)在这里。