删除while循环PHP中的重复项

时间:2017-11-22 16:10:12

标签: php mysql sql while-loop

我试图使用PHP从我的SQL查询中删除重复的结果。

表类别:

id | name
1 | sony
2 | nintendo

表子类别:

id | name | category_id
1 | playstation | 1
2 | playstation2 | 1
3 | wii | 2

表video_games

id | name | subcategories
1 | grand theft auto | 1,2
2 | super mario | 3

我的PHP代码:

$query = $database->query('SELECT id FROM subcategories WHERE category_id = "'.$_GET['id'].'"');
while($return = $query->fetch()) {
    $subcategories = $return['id'];
    $request = '%'.$subcategories.'%';
    $game_query = $database->prepare('SELECT * FROM video_games WHERE subcategories LIKE :request');
    $game_query->bindValue('request', $request);
    $game_query->execute();
    if($game_query->rowCount() > 0) {
        while($game_return = $game_query->fetch()) {
            echo $game_return['name'];
        }
    }
}

我的代码有效但有多个子类时我有重复的视频游戏。

我尝试使用SELECT DISTINCT * FROM video_games WHERE subcategories LIKE :request但同样的问题。

是否有任何想法使用SQL或PHP删除重复的结果?

3 个答案:

答案 0 :(得分:0)

您可以将视频游戏名称保存在阵列中,然后再打印。例如:

$games = array();
$query = $database->query('SELECT id FROM subcategories WHERE category_id = "'.$_GET['id'].'"');
while($return = $query->fetch()) {
    $subcategories = $return['id'];
    $request = '%'.$subcategories.'%';
    $game_query = $database->prepare('SELECT * FROM video_games WHERE subcategories LIKE :request');
    $game_query->bindValue('request', $request);
    $game_query->execute();
    if($game_query->rowCount() > 0) {
        while($game_return = $game_query->fetch()) {
            if (!in_array($game_return['name'], $games)) {
                $games[] = $game_return['name'];
            }
        }
    }
}
//now print the games
foreach ($games as $game) {
    echo $game;
}

编辑如果您想要的不仅仅是'名称',您可以使用$games组合展开$key => $value数组。例如:

    . . .
    while($game_return = $game_query->fetch()) {
        foreach ($games as $game) {
            if ($game['name'] === $game_return['name']]) {
                continue 2;
            }
        }
        $games[] = array(
            'name' => $game_return['name'],
            'price' => $game_return['price'],
        )
    }

然后将其打印出来:

foreach ($games as $game) {
    echo $game['name'];
    echo $game['price'];
}

答案 1 :(得分:0)

并且您也可以先使用 array_unique 保存数组中的视频游戏名称,然后删除重复值。

.
.
.
your code
.
.
 $game_query->execute();
 if($game_query->rowCount() > 0) {
      $fetch =array_unique($game_query->fetch());
         foreach ($fetch as $key => $value){
             echo $value;
      }
 }

测试

 <pre>
<?php
$arr=array("name"=>"1","name2"=>"2","name1"=>"2","name3"=>"3","name4"=>"1","name5"=>"2","name6"=>"3","name7"=>"22");
 print_r($arr);
 $fetch =array_unique($arr);
 print_r($fetch);
 foreach ($fetch as $key => $value)
            echo $key."=>".$value."<br>";

?>
 </pre>

答案 2 :(得分:0)

MySql能够通过自己的方式解决这个问题。

使用&#39; DISTINCT&#39;和&#39; FIND_IN_SET&#39;。

像这样:

"SELECT DISTINCT 
  vg.id,  
  vg.name, 
  vg.subcategories 
FROM 
  video_games vg
WHERE
  FIND_IN_SET( (SELECT id FROM subcategories WHERE category_id='".$_GET['id'])."' , vg.subcategories ) > 0 "