删除少于 5 个字的评论/内容
Posted
技术标签:
【中文标题】删除少于 5 个字的评论/内容【英文标题】:Delete Comments / Content with Less than 5 words 【发布时间】:2014-04-27 01:56:37 【问题描述】:我的网站上有很多无用的获批 cmets。我用来删除它们的一种方法是
DELETE FROM wp_comments WHERE comment_content LIKE '%agree%with%you,%thanks%, '
但是,这也会删除好的 cmets 并遗漏很多不好的 cmets。
如何修改查询以删除少于 5 个单词的 cmets。
由于实际数据库中的cmet数量很多,我担心是否要运行带有like
的命令,因为它会扫描超过20K行。有什么办法可以减轻负担?
【问题讨论】:
包含 5 个单词的评论通常有四个空格。 无论你使用什么技术,你总是要扫描 20K 行 @SteveMartin 因为comment_id 是递增值...我想知道是否可以多次运行查询,每次运行1K 行可能 是的,但这将增加 20K 次。无论如何,20K cmets 在整体方案中是一个非常微不足道的数字。您尝试过运行它吗? @SteveMartin Ya 我运行它...非常顺利...我需要更清楚地了解数据库方面的庞大数据。对不起,我的错。 【参考方案1】:尝试使用 LENGTH
。下面您将删除少于 35 个字符的条目。我认为它比使用文字更好。
length(comment_content) < 35 --//change length number as you want //35 characters
这样:
DELETE FROM wp_comments WHERE comment_content LIKE '%agree%with%you,%thanks%, '
AND length(comment_content) < 35
DEMO HERE
【讨论】:
【参考方案2】:您可以尝试计算字符数,将所有空格替换为空''
并减去它们。你需要CHAR_LENGTH()
和REPLACE
。
DELETE FROM wp_comments
WHERE (CHAR_LENGTH(comment_content) - CHAR_LENGTH(REPLACE(comment_content,' ',''))+1) < 5;
加 1 是一种调整,因为你的空间比单词少 1。
注意:CHAR_LENGTH()
用于mysql,您可以使用LENGTH()
用于其他人。
【讨论】:
【参考方案3】:例如:
SELECT LENGTH('this is a test')x,LENGTH(REPLACE('this is a test',' ',''))y;
+----+----+
| x | y |
+----+----+
| 14 | 11 |
+----+----+
14-11 = 3
3+1 = 4
This sentence has 4 words
【讨论】:
【参考方案4】:您可以尝试使用正则表达式,类似这样:
delete
from wp_comments
where not (MyComment REGEXP '[A-Za-z]+([[:space:]]+[A-Za-z]+)4,')
【讨论】:
【参考方案5】:最有效的方法是:
MySQL 查询开始:
-- Select All Rows
mysql> SELECT * FROM comments;
+----+---------------------------------------------------------------------------------------------------------------+---------------------+
| id | comments | log_time |
+----+---------------------------------------------------------------------------------------------------------------+---------------------+
| 1 | This Comment has 4 space | 2014-03-20 16:05:33 |
| 2 | Lorem ipsum dolor sit amet. | 2014-03-20 16:08:12 |
| 3 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laborum molest | 2014-03-20 16:08:12 |
| 4 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsa, eum, fuga dolorum cupiditate blanditiis enim | 2014-03-20 16:08:29 |
| 5 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Magnam | 2014-03-20 16:08:29 |
| 6 | Lorem ipsum dolor sit amet. | 2014-03-20 16:09:09 |
| 7 | Lorem ipsum dolor sit amet. | 2014-03-20 16:09:16 |
| 8 | Lorem ipsum dolor sit amet. | 2014-03-20 16:09:18 |
+----+---------------------------------------------------------------------------------------------------------------+---------------------+
8 rows in set (0.00 sec)
-- Check Space Count
mysql> SELECT comments, (
-> length( trim( comments ) ) - length( replace( trim( comments ) , ' ', '' ) )
-> ) AS total_space
-> FROM comments
-> LIMIT 0 , 30;
+---------------------------------------------------------------------------------------------------------------+-------------+
| comments | total_space |
+---------------------------------------------------------------------------------------------------------------+-------------+
| This Comment has 4 space | 4 |
| Lorem ipsum dolor sit amet. | 4 |
| Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laborum molest | 9 |
| Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsa, eum, fuga dolorum cupiditate blanditiis enim | 14 |
| Lorem ipsum dolor sit amet, consectetur adipisicing elit. Magnam | 8 |
| Lorem ipsum dolor sit amet. | 4 |
| Lorem ipsum dolor sit amet. | 4 |
| Lorem ipsum dolor sit amet. | 4 |
+---------------------------------------------------------------------------------------------------------------+-------------+
8 rows in set (0.00 sec)
-- Delete Those Records who Has less than 5 words
mysql> DELETE FROM comments WHERE (
-> length( trim( comments ) ) - length( replace( trim( comments ) , ' ', '' ) )
-> ) < 5;
Query OK, 5 rows affected (0.16 sec)
-- Select All Rows Again to Verify Rows
mysql> SELECT * FROM comments;
+----+---------------------------------------------------------------------------------------------------------------+---------------------+
| id | comments | log_time |
+----+---------------------------------------------------------------------------------------------------------------+---------------------+
| 3 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laborum molest | 2014-03-20 16:08:12 |
| 4 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsa, eum, fuga dolorum cupiditate blanditiis enim | 2014-03-20 16:08:29 |
| 5 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Magnam | 2014-03-20 16:08:29 |
+----+---------------------------------------------------------------------------------------------------------------+---------------------+
3 rows in set (0.00 sec)
在你的情况下,你可以这样使用:
-- Check Space Count
SELECT comment_content, (
length( trim( comment_content ) ) - length( replace( trim( comment_content ) , ' ', '' ) )
) AS total_space
FROM wp_comments
LIMIT 0 , 30;
-- Delete those comments who has less than 5 words
DELETE FROM wp_comments WHERE (
length( trim( comment_content ) ) - length( replace( trim( comment_content ) , ' ', '' ) )
) < 5;
-- Live DEMO
点击这里查看Live DEMO
【讨论】:
以上是关于删除少于 5 个字的评论/内容的主要内容,如果未能解决你的问题,请参考以下文章