douyong1908 2013-04-16 13:18
浏览 43
已采纳

DISTINCT函数无法与$ wpdb联机使用

I have a query which fetches some data from existing opencart web shop database.

The small problem are the doubled results which i solved locally using DISTINCT. It worked perfectly until i used the query online on my wordpress site. The results are doubled again, no matter the use of distinct.

The only difference in usage is that locally i used mysql() queries within a plain php document, however online i used $wpdb sentences inside the wordpress sidebar file.

Database is the same.

Local:

$preq = "
SELECT DISTINCT
    product.product_id as proid,
    product.image as proimg,
    product_description.name as proname 
FROM
    product, product_description
WHERE
    product_description.product_id = product.product_id
    AND FIND_IN_SET(product.product_id, ( SELECT value FROM setting where `key` = 'featured_product' ) )
";

if($res=mysql_query($preq)) {

while ($rss = mysql_fetch_array($res)) {

echo $rss['proid'];
echo "<br/><br/>";

}


}

Online:

$mydb= new wpdb(/connection info/);
            $rows = $mydb->get_results(
                "
                SELECT DISTINCT
                product.product_id as proid,
                product.image as proimg,
                product_description.name as proname 
                FROM
                product, product_description
                WHERE
                product_description.product_id = product.product_id
                AND FIND_IN_SET(product.product_id, ( SELECT value FROM setting where `key` = 'featured_product' ) )
                "
            );
            echo "<div id='fp_content'>";
            foreach ($rows as $obj) {
            echo $obj->proid;
            echo '<br/>';
            }
            echo "</div>";

Everything works fine except of the duplicates.

Results:

Locally:
Values in "value" column: 28,40,42,43,46,47,49

Results:
28

40

42

43

46

47

49

----------------------

Online:
Values in "value" column: 50,51,52

Results:
50
50
51
51
52
52
  • 写回答

1条回答 默认 最新

  • dongtun1683 2013-04-16 13:29
    关注

    Your query does return only non-duplicate values:

    SELECT DISTINCT
         product.product_id as proid,
         product.image as proimg,
         product_description.name as proname 
    FROM
         product, product_description
    WHERE
    

    But only your fields product.image or product_description.name will be different.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭