Solve the Problem of distinct failure when distinct and join both exist.

Source: Internet
Author: User

Introduction: This is a detailed page to solve the problem of distinct failure when distinct and join coexist. It introduces the knowledge, skills, experience, and some PHP Source Code related to PhP.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 337542 'rolling = 'no'>

See the following example:

$ SQL = 'select distinct (ontopid), GB. ID as ID, F. ID as FID, G. ID
GID, G. *, GB. *, F .*From pk_groupbuy GB

       Left joinPk_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 onidLimit
$ Start, $ num ";

Return tablesystem: Query ($ SQL );

Description of variables: $ arr_str is an array consisting of primary key IDs in pk_groupbuy. It is a string obtained through the explode function,

         
$ Start and $ num indicate the number of start records to be queried and the number of records to be queried.

Problem description: The ontopid in the pk_ontop table cannot be recorded as duplicated.

For example, I only need to query the ontopid that is placed on the top of the day in pk_ontop, that is, the product ID. Other product information is not required. If ontopid is found, it can be repeated, at this time, I can solve the problem by removing the array repeated elements, but if I want to query the corresponding product ID and the information in other associated tables, and according to the starttime, status, join is required for sorting paixu fields.
In the pk_ontop table, repeated issues are resolved and cannot be processed. In particular, repeated errors are not allowed in the API. What should I do? I don't know how to do this either. If someone else taught me how to do this, please refer:

 

   $ 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
$ Key => $ Val ){
     $ Topids [$ key] =
$ Val ['ontopid'];
    }
    $ Arr_str
= Implode (',', $ topids );
    $ Arr1 =
Tablesystem: Query ($ SQL );

$ SQL = 'select GB. Local, F. Phone, F. shopname
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
$ 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 ['shellerid'];
$ 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;

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/337542.html pageno: 9.

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.