从mysql查询中删除重复的camma

时间:2018-02-21 05:11:45

标签: php mysql

我正在尝试使用mysql IN运算符获取mulyiple值的数据,但问题是我得到了很多(,,,)。

以下是我的问题:

$get_id = "select rel_blog from pages";
$resu = mysql_query($get_id);
$p_id="";
$rel_b="";
$i = 1;
while ($page_id = mysql_fetch_array($resu)){

   $p_id.= $page_id['page_id']; 
  $rel_b.= $page_id['rel_blog']; 

  } 
$p_id."<br>";
echo $rel_b."<br>";

echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";

Ouery输出是这样的,这不是正确的形式,所以请帮助我。

  

SELECT * FROM pages WHERE rel_blog IN   (,114,93,126108default ,,,,默认,,,,默认,,默认,,,默认,,默认,,,,,,,默认,,默认,,,默认,,默认,,,,, ,,,,,,,,,,,,,,,,,,,,, 123,145,144,148,81,107default ,,,,,默认,,,,,, 145,138,118default ,,,,,,默认,,,, ,,,默认,,,,,,,默认,,,,,,,默认,,, 118default ,,,,默认,,,,,,,,默认,,,,,,,,,默认,,默认,,,,,,,,默认,,,,,,,,,默认,,默认,,,,,默认,,,,,,默认,,,,,,,,,默认,,,, ,,,,默认,,,,,默认,,,,,,, 123,122,125,113default ,,, ,,,默认默认情况下,, 124,145140default ,,,,,,,,,,,,,默认,,, ,,,,,默认,,, ,,,默认默认默认,,,, ,,,,默认,,默认,,,,默认,,, ,,,默认默认默认情况下,, ,,,,,,,默认,,,,,,默认,,,,默认,,,,,,默认,,,默认,,,, 137default ,,,,,,,默认,,, ,,,默认默认,,,,, ,默认,,,,,,,,,,默认,,,,,,,, 82,83default ,,,,,,默认,,,,默认,,,,默认,,, ,,,默认默认情况下, ,,,默认,,,,默认,,,,默认,,,,默认,,,默认,,,,默认,,,,默认,,, ,,,默认默认默认,,,,,, ,, ,,,,默认,,,,,默认,,,默认,,,,,默认,,,, ,默认,,,,默认,,,默认,,默认,,默认,,默认,,,,,,,默认,,,,默认,,默认,,,,默认,,,,默认,,,,默认,,,,,默认,,,,默认,,,,,默认,,,,,,,默认,,,,默认,,,,,默认,,,,,,默认,,,,,, ,默认,,,,,,默认,,,,,,,默认,,默认,,默认,,,,,默认,,,默认,,,默认,,,默认,,,,缺省情况下,114,默认,,,,,, 117,116default ,,,,,默认,,,, 117,116default ,,,,默认,,,,默认,,,,,默认,,,,默认,,,,,默认,,, ,默认,,,默认,,默认,,,,,,默认,,,,,,默认,,,,, 108default ,,,, 118,129,121,143,80,82,81,148,117,146,107,88,118,82,80,81,148,143,123,118,107,142,116,110,128,127,111,109,125,113,123,118,85,118,128,109,126 ,93,114,82,142,108,111,90,113,123,144,148,108,107,142,116,115,110,128,127,113,118,144,146,80,148,107,143,135,125,143,110,115,118,132,146,80,123,81,148,107,116,108,109,125,135,113,123,144,118,116,108,127,147,125,123,144,129,107,142,112,116,135,113,123,144,132,118,107,85,123,144,118,82,148,107,123,132,146,148,107,108,122,89,136,146,80,81,117,148,1 42,107,110,147,119,82,80,145,129,146,80,148,107,111,109,125,113,123,118,148,81,107,131,115,109,121,125,135,113,123,144,107,131,126,93,141,114,124,139,113,91,144,118,81,117,148,107,142,115,143,135,91,118,142,116,87,143,140,​​123,144,132,145,129,146,82,80,117,91,118,148,137,113,141,93,143,136,123,144,146,82,118,148,85,112,108,127,122,87,143,141,91,144,146,80,82,81,117,148,107,115,109,91,129,146,82,80,117,148,81,142,107,110,130,138, 89126113,默认情况下,默认情况下,118,146,82,81,148,107,142,115,109,126,默认情况下,123,144,145,80,146,82,81,117,142,128,138,135,93,140,​​123,144,145,132,82,80,117,83,142,130,140,​​145,129,82,80,81,108,111,138,89,143,125,145,123,118,82,80,81,117,88,112,108,147,139,119,140,​​123,144,118,80,82, 81,142,107,85,108,115,114,144,118,129,146,80,81,83,120,148,107,142,128,127,110,115,91,123,145,82,117,146,81,80,148,82,80,117,81,148,83,120,107,142,88,131,118,129,146,80,117,107,83,88,142,130,89,125,139,134,113,145,144,118,132,123,82,80,146,81,148,83,123,91,137,140,​​134,139,113,141,124,93140,15 0150)   按add_date desc limit 7

排序

3 个答案:

答案 0 :(得分:0)

实际上我无法弄清楚你是如何获得一个逗号的......这就是你可以尝试的方法。

        $get_id = "select rel_blog,page_id from pages";
        $resu = mysql_query($get_id);
        $p_id="";
        $rel_b="";
        $i = 1;
        while ($page_id = mysql_fetch_array($resu)){

          $p_id.= $page_id['page_id']; 

          if($page_id['rel_blog']!=="" && $page_id['rel_blog']==="default") {
            $rel_b.= $page_id['rel_blog'] .","; 
          }

        } 
        $p_id."<br>";

        $rel_b = substr($rel_b,0,-1); // get rid of the last comma
        echo $rel_b."<br>";

        echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7"; 

答案 1 :(得分:0)

您可以在查询之前执行此操作 $rel_b = preg_replace('/,+/', ',', trim($rel_b, ','));

这将删除重复的逗号以及字符串开头和结尾的逗号。

答案 2 :(得分:0)

尝试使用implode()

$get_id = "select rel_blog from pages";
$resu = mysql_query($get_id);
$p_id = array();
$rel_b = array();
$i = 1;
while ($page_id = mysql_fetch_array($resu)){
  $p_id[] = $page_id['page_id']; 
  $rel_b[] = $page_id['rel_blog'];
}

$p_id = implode(',', $p_id);
$rel_b = implode(',', rel_b);

echo $getblog = "SELECT * FROM pages WHERE rel_blog IN ($rel_b) order by add_date desc limit 7";