解決當distinct和join同時存在distinct失效問題_PHP教程

來源:互聯網
上載者:User
$sql = 'select distinct(ontopid),gb.id as id,f.id as fid,g.id as gid,g.*,gb.*,f.* from pk_groupbuy gb

left join pk_ontop as o on o.ontopid=gb.id

left join pk_goods g on gb.goodsid=g.id and g.status=2 and g.invalid>UNIX_TIMESTAMP()

left join pk_fastgroupbuy f on gb.fastgroupbuyid=f.id

where gb.id in ('.$arr_str.') and (gb.status="2" or gb.status="3")

and gb.endtime>UNIX_TIMESTAMP() and gb.starttime

group by onid limit $start,$num";

return TableSystem::query($sql);

變數說明:$arr_str是一個由pk_groupbuy中主鍵 id組成的一個數組,經過explode函數得到的字串,

$start,$num分別是查詢的開始記錄數,和要查詢的記錄數。

問題說明:pk_ontop表中ontopid在不能記錄中有重複現象

比如:我只需要查詢出來pk_ontop中當天置頂的ontopid,即商品id,不需要其他的商品資訊,查詢出來的有ontopid就算有重複現象,這時我可以通過去除數組重複元素解決問題,但是如果我要查詢出相應商品id並查詢其他相關聯表中的資訊,並按照ontop表中starttime,status,paixu欄位進行排序等操作時,就需要join pk_ontop表,所以之前解決的重複問題就又會出現,無法處理,特別是在api中,是不允許出現重複的,這要怎麼辦呢?我也不會額,別人教我這樣弄,請大家參考下:

$sql = 'SELECT DISTINCT(ontopid),starttime,paixu FROM pk_ontop ORDER BY starttime DESC,STATUS ASC,paixu ASC LIMIT 17';
$arr = TableSystem::query($sql);
foreach($arr as $key=>$val){
$topids[$key] = $val['ontopid'];
}
$arr_str = implode(',',$topids);
$arr1 = TableSystem::query($sql);

$sql = 'select gb.local,f.phone,f.shopname as fshopname,gb.maxnum,gb.intro,gb.buynum,g.pic,f.googleaddress,gb.goodsclassid,gb.sellerid,f.img,gb.province,gb.city,gb.id,gb.title,g.pic,gb.starttime,
gb.endtime,gb.price,gb.goodsprice from pk_groupbuy gb
left join pk_goods g on gb.goodsid=g.id and g.status=2 and g.invalid > UNIX_TIMESTAMP()
left JOIN pk_fastgroupbuy f ON f.id=gb.fastgroupbuyid
where (gb.status="2" or gb.status="3") and gb.endtime > UNIX_TIMESTAMP()
and gb.starttime < UNIX_TIMESTAMP() AND gb.id in ('.$arr_str.')';

$arr2 = TableSystem::query($sql);
foreach($arr2 as $key=>$val){
$local[$val['id']] = $val['local'];
$phone[$val['id']] = $val['phone'];
$fshopname[$val['id']] = $val['fshopname'];
$maxnum[$val['id']] = $val['maxnum'];
$intro[$val['id']] = $val['intro'];
$buynums[$val['id']] = $val['buynum'];
$fgoogleaddresss[$val['id']] = $val['googleaddress'];
$goodsclassid[$val['id']] = $val['goodsclassid'];
$sellids[$val['id']] = $val['sellerid'];
$provices[$val['id']] = $val['province'];
$citys[$val['id']] = $val['city'];
$titles[$val['id']]= $val['title'];
$pics[$val['id']] = $val['pic'] ? $val['pic'] : $val['img'];
$starttimes[$val['id']] = $val['starttime'];
$endtimes[$val['id']] = $val['endtime'];
$prices[$val['id']] = $val['price'];
$goodsprices[$val['id']] = $val['goodsprice'];
}
unset($arr2);
foreach($arr1 as $key=>$val){
$list[$key]['id'] = $val['ontopid'];
$list[$key]['province'] = $provices[$val['ontopid']];
$list[$key]['city'] = $citys[$val['ontopid']];
$list[$key]['title'] = $titles[$val['ontopid']];
$list[$key]['pic'] = $pics[$val['ontopid']];
$list[$key]['starttime'] = $starttimes[$val['ontopid']];
$list[$key]['endtime'] = $endtimes[$val['ontopid']];
$list[$key]['price'] = $prices[$val['ontopid']];
$list[$key]['goodsprice'] = $goodsprices[$val['ontopid']];
$list[$key]['sellerid'] = $sellids[$val['ontopid']];
$list[$key]['fgoogleaddress'] = $fgoogleaddresss[$val['ontopid']];
$list[$key]['goodsclassid'] = $goodsclassid[$val['ontopid']];
$list[$key]['buynum'] = $buynums[$val['ontopid']];
$list[$key]['intro'] = $intro[$val['ontopid']];
$list[$key]['maxnum'] = $maxnum[$val['ontopid']];
$list[$key]['fshopname'] = $fshopname[$val['ontopid']];
$list[$key]['fphone'] = $phone[$val['ontopid']];
$list[$key]['local'] = $local[$val['ontopid']];
}
return $list;

http://www.bkjia.com/PHPjc/630743.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630743.htmlTechArticle$sql = 'select distinct(ontopid),gb.id as id,f.id as fid,g.id as gid,g.*,gb.*,f.* from pk_groupbuy gb left join pk_ontop as o on o.ontopid=gb.id left join pk_goods g on gb.goodsid=...

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.