mysql查询不断返回重复的结果

时间:2013-07-08 08:51:50

标签: php mysql duplicates opencart

我有一个返回重复结果的查询。这是:

$product_sql_test2 = $this->db->query("SELECT p.product_id, p.price,    pc.product_id AS product_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 pc.product_id
ORDER BY RAND( ) 
LIMIT 0 , 10");

我曾尝试使用array_unique函数,但它没有做任何事情。可以做些什么?

P.S。我以前曾问过类似的问题,但没有回答它的链接是: previous question with full source code.

致以最诚挚的问候 波格丹

p.p.s。源代码现在

if(isset($product_info['related_kv4nt_id_2'])) 
{
    $i=1;
    $imax = 5;
    while ($i < $imax) 
    {
        $product_sql_test2 = $this->db->query("SELECT DISTINCT p.product_id, p.price,    pc.product_id AS product_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 pc.product_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++;
    }
} 

1 个答案:

答案 0 :(得分:0)

尝试在select语句http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html

中添加DISTINCT子句