MySQL The basic idea of removing duplicate query SQL statement

Source: Internet
Author: User

SELECTR.*  fromTrans_flow R, (SELECTOrder_no,MAX(Status_time) asStatus_time fromTrans_flowGROUP  byorder_no) SWHERER.order_no=S.order_no andR.status_time=S.status_timeORDER  byR.status_timeDESC SELECTA.order_no,a.mem_no,c.card_no,c.card_holder,c.bank_name,c.branch_bank_name,a.flag, A.income_type_txt,a.amt, A.create_time fromAccount_log aJOIN(SELECTB.*,MAX(ID) fromAccount BGROUP  byb.mem_no) C onDate_add (A.create_time, INTERVAL7  Day)<Now () andA. ' Mem_no '=C.mem_noORDER  bya.IDdesc   

Workaround:
The structure is as follows:
type name guest       date
1   aa        sa           20131101
1    bb      dsf           20131102
1   aa      wew         20131103
I want to group by name, in descending order of date, and I want the result as follows:
type name guest   date
1   aa        wew     20131103
1   bb       DSF        20131102

select r.* from tab  R, ( Select Name, max (date) From the tab group by name) S
where R.name=s.name and R.date=s . Date
order by r.date desc

MySQL Remove duplicate query SQL statement basic idea

Related Article

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.