The SQL statement that kneels to the left join

Source: Internet
Author: User
Kneeling for a LEFT JOIN SQL statement notation
Table Goods:gid | Name
1 Xde
2 DFDD
3 Ancient Oriental style
Table Glist:lid | GID | SubName | Displayorder
1 133,442
2 1 sweep sweeping 12
3 Illegal 2nd-party 10
4 2 to Karma multiple 1
I now want to remove the lid in the Glist table as 2, 32 items.
But select G. *, F. * FROM goods G left JOIN glist f on g.gid = F.gid ORDER by G.displayorder DESC limit 2 The result of this SQL fetch is wrong.

According to goods in the GID in ascending order to remove two and also take out in the glist belongs to the corresponding GID (the Displayorder value of the largest one)
The result should be goods in the glist and also take out the 2,3 in the

------Solution--------------------
It can be written like this. Select A.*,max (B.displayorder), b.subname from goods as a LEFT join Glist as B on A.gid=b.gid GROUP by A.gid
Group Query Displayorder Field maximum value
------Solution--------------------
SELECT G. *, F. * FROM goods G left JOIN glist f in g.gid = F.gid ORDER by F.displayorder DESC limit 2

There's a problem with your SQL, not G, it's F. But that doesn't meet your requirements.
"According to goods in the GID in ascending order to remove two and also take out in the glist belongs to the corresponding GID (the Displayorder value of the largest one)
The result should be goods in the same time also take out glist in the 2,3 "

Upstairs is also wrong, although Max (B.displayorder) is correct, but b.subname wrong.

The correct one should be:
SELECT * FROM (select G.gid, G.name, f.lid,f.subname,f.displayorder to goods G left joins Glist F on G.gid=f.gid ORDER by G.gid ASC, F.displayorder DESC) H Group by H.gid LIMIT 2
  • 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.