php - 无法匹配mysql中的字符串

标签 php mysql

我在匹配字符串以查找数据库中的重复记录时遇到困难。

如果你看,你会看到我选择评论列等于此评论的评论行

我将每条评论分别用于它的记录,但我想我会得到 2 条评论 ID 分别为 2254915 和 50712 的记录,但它们似乎只查询各自的评论。

我没有找到关于这 2 个查询的不同之处,因为评论是相同的。

    mysql> select * from comments where comment="35 dancers on the roster,  but if you head in there at 4 or 5 in the afternoon,  you might find 3 chicks.  The only thing I'd change in this place would be the seating-the same filthy chairs for the last ten years";
    +-----------+----------+---------+------------+----------+-------+--------+-----------+--------+------------+---------+--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+----------+--------+--------+
    | commentid | parentid | refno   | createdate | remoteip | locid | clubid | profileid | userid | legacyuser | mediaid | status | comment                                                                                                                                                                                                               | likes | dislikes | import | author |
    +-----------+----------+---------+------------+----------+-------+--------+-----------+--------+------------+---------+--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+----------+--------+--------+
    |   2254915 |        0 | 2519833 | 1328361720 |          |  3166 |  18950 |         0 |      0 |    1846155 |       0 |      1 | 35 dancers on the roster,  but if you head in there at 4 or 5 in the afternoon,  you might find 3 chicks.  The only thing I'd change in this place would be the seating-the same filthy chairs for the last ten years |     0 |        0 |      1 | Jess   |
    +-----------+----------+---------+------------+----------+-------+--------+-----------+--------+------------+---------+--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+----------+--------+--------+
    1 row in set (2.96 sec)

    mysql> select * from comments where comment="35 dancers on the roster, but if you head in there at 4 or 5 in the afternoon, you might find 3 chicks. The only thing I'd change in this place would be the seating-the same filthy chairs for the last ten years";
    +-----------+----------+---------+------------+----------+-------+--------+-----------+--------+------------+---------+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+----------+--------+--------+
    | commentid | parentid | refno   | createdate | remoteip | locid | clubid | profileid | userid | legacyuser | mediaid | status | comment                                                                                                                                                                                                            | likes | dislikes | import | author |
    +-----------+----------+---------+------------+----------+-------+--------+-----------+--------+------------+---------+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+----------+--------+--------+
    |     50712 |        0 | 2519833 | 1328343720 |          |  3166 |  18950 |         0 |  28128 |          0 |       0 |      1 | 35 dancers on the roster, but if you head in there at 4 or 5 in the afternoon, you might find 3 chicks. The only thing I'd change in this place would be the seating-the same filthy chairs for the last ten years |     0 |        0 |      0 | Jess   |
    +-----------+----------+---------+------------+----------+-------+--------+-----------+--------+------------+---------+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+----------+--------+--------+
    1 row in set (2.39 sec)

不太清楚这里发生了什么。这是我的代码:

    require_once("../www/api");
    $locid=3166;
    $sql="select commentid,legacyuser,userid,comment,status from comments where /**datediff(now(),from_unixtime(createdate))>=60 and*/ locid=$locid and status>=1 order by createdate desc limit 20";
    $rs=mysqli_query($dbh,$sql);
    $cnt=0;
    while($row=mysqli_fetch_assoc($rs)){
      if($cnt<10){
        $cnt++;
        continue;
      }
      $cnt++;
      // variables declaration
      $commentid=(int)$row['commentid'];
      echo $commentid.'-';
      $legacyuser=(int)$row['legacyuser'];
      $userid=(int)$row['userid'];
      $comment=mysqli_real_escape_string($dbh,$row['comment']);


      $status=$row[status];
      //echo $comment;

      // get active duplicates count
      //echo "select count(commentid) as count from comments where comment='$comment' and locid=608 and status!=-14"."\r\n";
      $cnt=mysqli_fetch_assoc(mysqli_query($dbh,"select count(commentid) as count from comments where comment='$comment' AND locid=$locid AND status!=-14"));
      $cntActive=(int)$cnt[count];
      //die($cntActive);
      if($cntActive==1)continue;

      if($cntActive>1){
        //$comment=mysqli_real_escape_string($dbh,$comment);
        $activers=mysqli_query($dbh,"select commentid from comments where comment='$comment' AND locid=$locid AND status!=-14");
        $icnt=0;
        while($irow=mysqli_fetch_assoc($activers)){
          //print_r($irow);
          if($icnt==0){
            $icnt++;
            continue;
          }
          $commentid=$irow['commentid'];
          echo "update comments set status=-14 where commentid=$commentid";
          mysqli_query($dbh,"update comments set status=-14 where commentid=$commentid");
          $icnt++;
        }
      }
    }

据我所知,它绕过了大于 1 的计数,我们可以看到有 2 个不同的记录,但评论是相同的,所以我不知道为什么。

最佳答案

两个评论不一样。第一个比第二个多一个空格字符。以下内容会提醒您注意这一事实,当两件事看起来很相似但您不确定它们是否相似时,这应该是您的第一个“去向”。

SELECT MD5(comment) FROM comments WHERE commentid IN (50712,2254915);

由于空白是此处唯一的区别,您可能希望按照以下行删除该空白:

SELECT REPLACE(comment, ' ', '') FROM comments;

这绝不是“足够接近”的匹配。您需要为此定义更好的规则并实现自定义算法。

关于php - 无法匹配mysql中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34231650/

相关文章:

mysql - 删除 MySQL 中连续重复行的有效方法

php - 如何获取数组中的随机元素以及如何从mysql中获取随机行?

PHP Laravel GD 库扩展在此 PHP 安装中不可用,但在 PHP Ini 中

php - 有效地保存表中的数据,并且易于检索和回显

javascript - 来自数据数组的 Highcharts 类别

php - 如何在数据库中搜索字符串 X 次? (MySQL + PHP)

php - 在 Javascript 中通过 PHP 设置确定用户的角色

javascript - Laravel 5,4 JavaScript 函数与 ajax

php - mysql:优化sql查询和慢查询

mysql - 如何划分分组的计算结果?