Solve the problem of distinct failure when distinct and join are present

Source: Internet
Author: User
Tags foreach

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

Variable Description: $arr _str is an array of primary key IDs in Pk_groupbuy, a string obtained by the explode function,

$start, $num is the number of start records for the query, and the number of records to query.

Problem Description: Pk_ontop table in the ontopid can not be recorded in duplicate phenomenon

For example: I only need to query out Pk_ontop in the day of the Ontopid, that is, the product ID, do not need other merchandise information, query out of the ontopid even if there is duplication, then I can remove the array of repeated elements to solve the problem, However, if I want to query the corresponding Product ID and query the information in other related tables, and sort by the Starttime,status,paixu field in the Ontop table, we need to join the Pk_ontop table, so the repeated problems resolved before will appear again, unable to handle , especially in the API, is not allowed to appear duplicates, how to do? I also will not amount, others teach me to do so, please refer to the following:

$sql = ' SELECT DISTINCT (ontopid), Starttime,paixu from Pk_ontop order by StartTime Desc,status ASC Asc,paixu 17 ';
$arr = Tablesystem::query ($sql);
foreach ($arr as $key => $val) {
$topids [$key] = $val [' Ontopid '];
}
$arr _str = Implode (', ', $topids);
$arr 1 = 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. ') ';

$arr 2 = tablesystem::query ($sql);
foreach ($arr 2 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 ($arr 2);
foreach ($arr 1 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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.