$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<UNIX_TIMESTAMP()
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;