Check if there is a double value in the while

0

I'm at half a day trying to do this.

I have this loop

if(@mysqli_num_rows($sql_es_1_aux) == 1){
   echo "";
}else{
   $sql_es_1 = mysqli_query($config, "SELECT peso FROM tb_sub_agrupamento 
                                      WHERE id_ctr IN ($id_ctr_ok) AND 
                                       peso <> ''") or die(mysqli_error($config));


  if(@mysqli_num_rows($sql_es_1) <= '0'){
    echo "";
  }else{

       while($r_sql_es_1 = mysqli_fetch_array($sql_es_1)){

           $peso_sel_es1 = $r_sql_es_1[0];                                                  
        }
    }
}

$ peso_sel_es1 can be repeated.

Is there any way to check if it is repeated outside the while?

If I turn it into array (by putting []) help?

I need to determine if it is repeated, do not show the weight.

    
asked by anonymous 22.04.2018 / 19:37

1 answer

1

You can solve in your query by grouping the weights. And counting how many of them there are. Then in PHP you can compare if the repetition is greater than 1.

SELECT 
    peso, 
    COUNT(peso) AS repeticoes 
FROM 
    tb_sub_agrupamento 
WHERE 
    id_ctr IN ($id_ctr_ok) 
    AND 
    peso <> '' 
GROUP BY 
    peso

Or if you do not want to display repeated weights, only those that are unique. The query also solves this. Having no need for business rule in source.

SELECT 
    peso, 
    COUNT(peso) AS repeticoes 
FROM 
    tb_sub_agrupamento 
WHERE 
    id_ctr IN ($id_ctr_ok) 
    AND 
    peso <> '' 
GROUP BY 
    peso
HAVING
    repeticoes = 1
    
22.04.2018 / 20:35