Beg for a leftjoin SQL statement writing table goods: gid | name & nbsp; 1xde & nbsp; 2 dfdd & nbsp; 3 ancient oriental style table glist: lid | gid | subname | description of a left join SQL statement written by displa
Table goods: gid | name
1 xde
2 dfdd
3. Ancient Oriental style
Table glist: lid | gid | subname | displayorder
1 1 3 3 4 4 2
2 1 sweep the floor 12
3 2 the Japanese side is illegal 10
4 2 to merge multiple 1
Now I want to retrieve the two lids in the glist table: 2 and 3.
But SELECT g. *, f. * FROM goods g left join glist f ON g. gid = f. gid order by g. displayorder DESC limit 2 is returned incorrectly.
Retrieve two Gids in the goods ascending order and one that belongs to the corresponding gid in the glist (take the largest displayorder value)
The result is 1, 2, and 2 in glist.
------ Solution --------------------
You can write 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
Maximum value of displayorder field in group query
------ Solution --------------------
SELECT g. *, f. * FROM goods g left join glist f ON g. gid = f. gid order by f. displayorder DESC limit 2
Your SQL statement is faulty, not g, but f. However, this does not meet the following requirements:
"Retrieve two items in ascending order of the gid in goods and one that belongs to the corresponding gid in glist (take the largest displayorder value)
The result should be 1 or 2 in goods, and 2 and 3 in glist are also retrieved"
The upstairs is also wrong. although max (B. displayorder) is correct, B. subname is incorrect.
Which of the following statements is true:
Select * from (select g. gid, g. name, f. lid, f. subname, f. displayorder from goods g left join glist f on g. gid = f. gid order by g. gid ASC, f. displayorder DESC) h group by h. gid LIMIT 2
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