Implementation of the first N records for the GROUPBY group in mysql

Source: Internet
Author: User
How to obtain the first two records of each group after GROUPBY is described in the following section.

The following describes how to obtain the first two records of each GROUP after group by in mysql.

The Code is as follows:

Mysql> select * from t_subject;
| Id | uid | subject |
| 1 | 1 | aa |
| 2 | 2 | bb |
| 3 | 3 | cc |
| 4 | 4 | dd |
| 5 | 2 | ee |
| 6 | 2 | rr |
| 7 | 3 | tt |
| 8 | 2 | yy |
| 9 | 3 | qq |
| 10 | 4 | oo |
| 11 | 3 | pp |
| 12 | 3 | kk |
| 13 | 1 | mm |
| 14 | 4 | nn |
| 15 | 1 | ss |
| 16 | 4 | vv |
| 17 | 1 | ff |

SELECT uid, group_concat (subject)
FROM (SELECT id, uid, subject
FROM (SELECT id, uid, subject,
(Select count (*)
FROM t_subject
WHERE uid = t. uid
AND subject <= t. subject) RK
FROM t_subject t) t1
WHERE rk <= 3) t2
Group by uid

| Uid | group_concat (a. subject) |
| 1 | aa, ff, ss, mm |
| 2 | yy, rr, ee, bb |
| 3 | kk, pp, xx, qq, tt, cc |
| 4 | nn, dd, vv, oo |

---- What I want is how to write the following results (that is, retrieve the first three records of each group after the group )---

The Code is as follows:
| Uid | group_concat (a. subject) |
| 1 | aa, ff, mm |
| 2 | yy, rr, ee |
| 3 | kk, pp, xx |
| 4 | nn, dd, vv |

Instance 2

Implement group by grouping of the First N records in mysql

Mysql group, retrieve records

The following describes how to obtain the first two records of each GROUP after group by in mysql.


This is a test table (I don't know how to think about it. At that time, the table name was directly typed into aa, Khan ~~~~) :


Result:



Method 1:

The Code is as follows:
SELECT a. id, a. SName, a. ClsNo, a. Score FROM aa a left join aa B ON a. ClsNo = B. ClsNo AND a. Score

Split analysis:

1, Left join aa B ON a. ClsNo = B. ClsNo AND a. Score < p><>

In the same class (four persons in each class), the score is higher than the current student's record, which means that the students with the lowest score will generate three records

2, Group by a. id, a. SName, a. ClsNo, a. Score having count (B. id) <2

A. id,. SName,. clsNo,. score can represent a student (grouped by student). If count (B. id) <2 (no more than two people have scored more than you), then only the first and second places are left.


Method 2:

The Code is as follows:
SELECT * FROM aa a WHERE 2> (select count (*) FROM aa WHERE ClsNo = a. ClsNo and Score> a. Score) order by a. ClsNo, a. Score DESC;

I think this is quite interesting. Take every record and judge whether the number of students in the same class is less than two if they exceed the current score.


Method 3:

The Code is as follows:
SELECT * FROM aa WHERE id IN (SELECT id FROM aa WHERE ClsNo = a. ClsNo order by Score desc limit 2) order by a. ClsNo, a. Score DESC;

This method fails the test. ERROR 1235 (42000): This version of MySQL doesn't yet support 'limit & IN/ALL/ANY/SOME subquery ', limit cannot be used in these subqueries.

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.