Page 1 of 1

fighting duplicate query results please help

Posted: Wed Jul 10, 2013 5:17 pm
by bogdanus91
Hello all. I am writing somethink like similar products. Products are taking from database based on price range specified by administrator. Only problem with that is the query returns duplicate data. One product can be shown 2 or even more times. I tried to use SELECT DISTINCT, Group by in MySQL and array_unique function in PHP. Neither didn't work at all. What are some other ways to solve the problem? The code is below.

Code: Select all

if(isset($product_info['related_kv4nt_id_2'])) {
   $i=1;
                        $imax = 5;
                        while ($i < $imax) {
                      $product_sql_test2 = $this->db->query("SELECT  p.product_id, p.price,    pc.product_id AS p_id
FROM oc_product p
INNER JOIN oc_product_to_category pc ON p.product_id = pc.product_id
WHERE p.price  BETWEEN '".$product_info['related_kv4nt_min_2']."' and '".$product_info['related_kv4nt_max_2']."'
AND pc.category_id =  '".$product_info['related_kv4nt_id_2']."'
GROUP BY p_id
ORDER BY RAND( ) 
LIMIT 0 , 10");
 
                            if(isset($product_sql_test2->row['product_id'])){$this->data['product_spec_2_'.$i.''] = $product_sql_test2->row['product_id'];

}
 
  
$i++;

}
}

Re: fighting duplicate query results please help

Posted: Wed Jul 10, 2013 6:53 pm
by pedro1993
What is related_kv4nt_id_2? What are you trying to achieve?

Peter

Re: fighting duplicate query results please help

Posted: Wed Jul 10, 2013 7:09 pm
by bogdanus91
Hello. The related products are divided in 4 categories defined by administrator. For example if I am choosing a video card I will see in my related products motherboards, Hard discs, cooling systems ets. That is defined by administrator too. The name $product_info['related_kv4nt_id_2'] is the category id in this case it is second category. While products are shown properly problem is that there are could be 2 absolutely same products with same price. I just need to avoid it from happening.