无法匹配mysql中的字符串

时间:2015-12-11 20:03:56

标签: php mysql

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

如果你看,你会看到im selecting the comment row where the comment column equals this comment

我使用了每个评论各自的记录,但我认为我会得到2条记录2254915和50712,而他们似乎只是查询他们各自的评论。

我发现这两个查询有些不同,因为评论是相同的。

    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个不同的记录,但评论是相同的,所以我不知道为什么。

2 个答案:

答案 0 :(得分:0)

两条评论不一样。第一个中有一个空格字符,而第二个空格字符。以下内容会提醒您注意这一事实,应该是您的第一个&#34;转到&#34;当两件事看起来很相似但你不确定他们是不是或不是。

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

由于空格是此处唯一的区别,您可能希望删除该空格,如下所示:

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

这绝不是近似的足够接近&#34;比赛。您需要为此定义更好的规则并实现自定义算法。

答案 1 :(得分:0)

仔细看一下sql语句,记录中有一个额外的空格。

import numpy as np
a = np.arange(1,11,1)
print(a)
print(a[::3])