如何从句子mysql查询中选择#开头#

时间:2015-10-11 17:32:48

标签: mysql sql

让我们简要解释一下

这是一个非常新的主题,我想只从#句子

中取#来获取特定的单词 我有句话

Hi Majjx Uxud Xhhxhd Hx Dhx #hdhd Jdhhdhshhfd Hxhhd @bhd Hxhd Hxhhd Dhhdh www.myinnos.in Hdhd Xfhhxhd Xhhdh Xhx 9560233669 ndhdh Hxhhdh Dhh

从上面的句子我必须获取#hdhd

为我的问题找到了解决方案,现在我想计算并将重复的单词显示为计数

select val from(
select (substring_index(substring_index(a, ' ', n.n), ' ', -1)) val
from (select id, message as a from filmbooknewsfeed) t
cross join(
 select a.n + b.n * 10 + 1 n
 from 
    (select 0 as n union select 1 union select 2 union select 3 union select 4 union
        select 5 union select 6 union select 7 union select 8 union select 9
    ) a,
    (select 0 as n union select 1 union select 2 union select 3 union select 4 union
        select 5 union select 6 union select 7 union select 8 union select 9
    ) b
    order by n
) n
where n.n <= 1 + (length(t.a) - length(replace(t.a, ' ', '')))
order by val asc
)x where val like '#%' 

2 个答案:

答案 0 :(得分:1)

正如我所说,你需要将句子转换成行。以防万一,如果在句子中你有超过1个单词以#开头。

select val from(
select (substring_index(substring_index(a, ' ', n.n), ' ', -1)) val
from (
    select 'Hi Majjx Uxud Xhhxhd Hx Dhx #hdhd Jdhhdhshhfd Hxhhd @bhd Hxhd Hxhhd Dhhdh www.myinnos.in Hdhd Xfhhxhd Xhhdh Xhx 9560233669 ndhdh Hxhhdh Dhh' as a
) t 
cross join(
 select a.n + b.n * 10 + 1 n
 from 
    (select 0 as n union select 1 union select 2 union select 3 union select 4 union
        select 5 union select 6 union select 7 union select 8 union select 9
    ) a,
    (select 0 as n union select 1 union select 2 union select 3 union select 4 union
        select 5 union select 6 union select 7 union select 8 union select 9
    ) b
    order by n
) n
where n.n <= 1 + (length(t.a) - length(replace(t.a, ' ', '')))
order by val asc
)x where val like '#%'

会给你#hdhd。即使你在句子中有超过1 #。这会给你正确的结果。

修改
如果你想按结果分组并按照Twitter交易主题等大多数出现的词排序,请像这样修改你的查询(作为对问题的查询)

select val,count(val) as cnt from(
    select (substring_index(substring_index(a, ' ', n.n), ' ', -1)) val
    from (select id, message as a from filmbooknewsfeed) t
    cross join(
     select a.n + b.n * 10 + 1 n
     from 
            (select 0 as n union select 1 union select 2 union select 3 union select 4 union
                    select 5 union select 6 union select 7 union select 8 union select 9
            ) a,
            (select 0 as n union select 1 union select 2 union select 3 union select 4 union
                    select 5 union select 6 union select 7 union select 8 union select 9
            ) b
            order by n
    ) n
    where n.n <= 1 + (length(t.a) - length(replace(t.a, ' ', '')))
    order by val asc
)x where val like '#%' 
group by val 
order by cnt desc

答案 1 :(得分:0)

您可以使用substring_index()

执行此操作
select substring_index(substring_index(substring_index(sentence, '#', 2), '#', -1), ' ', 1)