【问题标题】:mysql count number of hashtags through tablemysql通过表计算主题标签的数量
【发布时间】:2015-02-08 20:26:45
【问题描述】:

我有带有值的 mysql 表文本(id,文本):

1, 'some #text with #hashtags ...'
2, 'more #text without....'
... and more

可以在 sql 中获取任何主题标签的计数吗? 某事

like select count(regexp) from text where...regexp... magic happened ;)

需要的结果:#text ...2x, $#hashtags 1x

我无法用

选择文本
select * from  table where text REGEXP '#[a-z]*

但是这样我只得到带有任何#hashtag的行

有什么想法吗?

好吧,我当然可以全选并用php计算结果,但它可能只有mysql解决方案?

【问题讨论】:

    标签: php mysql regex


    【解决方案1】:
    1. 从 MySQL 中选择行 SELECT text FROM text WHERE text REGEXP '#(\w+)'

    \w 表示Any word character (letter, number, underscore)

    + 表示one or more

    1. text column 获取主题标签 使用 preg_match 使用相同的模式 '#(\w+)' 并使用 $match(查看参考!

    $匹配 =

    Array
    (
        [0] => #hashtag
        [1] => hashtag
    )
    
    1. 在数组中计数$hashtags_count[$match[1]]++

    【讨论】:

      猜你喜欢
      • 2020-02-17
      • 1970-01-01
      • 2020-11-26
      • 2015-07-08
      • 2011-02-04
      • 1970-01-01
      • 2012-07-14
      • 2020-06-01
      • 1970-01-01
      相关资源
      最近更新 更多