MySQL计算文本字段中短语列表的使用情况

时间:2017-03-29 08:46:33

标签: mysql

寻找一个MySQL查询,让我可以计算文本列中短语列表的用法。

例如;

+--------------------+---------------------+------+-----+---------+
| Field              | Type                | Null | Key | Default | 
+--------------------+---------------------+------+-----+---------+
| smilie_id          | int(10) unsigned    | NO   | PRI | NULL    | 
| smilie_text        | text                | NO   |     | NULL    |                

......和......

+-------------------+---------------------------------------+------+-----+-------+
| Field             | Type                                  | Null | Key | Default 
+-------------------+---------------------------------------+------+-----+-------+
| post_id           | int(10) unsigned                      | NO   | PRI | NULL    
| message           | mediumtext                            | NO   |     | NULL    

我想计算smilie_text在消息中出现的次数。

感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用以下查询执行此操作:

SELECT  CAST((
                CHAR_LENGTH('smilie_text This is the smilie_text search text ssmilie_text') -
               CHAR_LENGTH(REPLACE('smilie_text This is the smilie_text search text ssmilie_text','smilie_text',''))
            ) / CHAR_LENGTH('smilie_text') AS UNSIGNED );

<强>样品

mysql> SELECT  CAST((
    ->                 CHAR_LENGTH('smilie_text This is the smilie_text search text ssmilie_text') -
    ->                CHAR_LENGTH(REPLACE('smilie_text This is the smilie_text search text ssmilie_text','smilie_text',''))
    ->             ) / CHAR_LENGTH('smilie_text') AS UNSIGNED ) as result;
+--------+
| result |
+--------+
|      3 |
+--------+
1 row in set (0,00 sec)

mysql>