How to realize grouping limit by SQL
This article mainly explains "how to realize grouping limit by SQL". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to realize grouping limit by SQL".
Mysql > create table a (id int,fid int, name varchar (10))
Query OK, 0 rows affected (0.10 sec)
Mysql > insert into a value (1, 1, 1, 5)
Query OK, 1 row affected (0.04 sec)
Mysql > insert into a value (2, 1, 1, 2, 5, 5, 5, 5, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
Query OK, 1 row affected (0.01sec)
Mysql > insert into a value (3, 1, 3, 5, 3, 5, 3, 5, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
Query OK, 1 row affected (0.01sec)
Mysql > insert into a value (4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 5, 4, 4, 4, 4, 5, 4, 4, 4, 5, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4,
Query OK, 1 row affected (0.02 sec)
Mysql > insert into a value (5, 5, 2, 5, 5, 5, 2, 5, 5, 2, 5, 5, 5, 5, 5, 2, 5, 4, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
Query OK, 1 row affected (0.01sec)
Mysql > insert into a value (6, 2, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 4, 4, 4, 4, 4, 6, 4, 4, 4, 4, 4, 6, 4, 4, 4, 6, 4, 4, 4, 4, 6, 4, 4, 4, 4, 4, 4, 4, 4, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 4, 4, 4, 4, 4, 4, 4, 6, 4, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
Query OK, 1 row affected (0.00 sec)
Mysql > select * from a
->
+-+
| | id | fid | name | |
+-+
| | 1 | 1 | a |
| | 2 | 1 | b | |
| | 3 | 1 | c | |
| | 4 | 2 | d | |
| | 5 | 2 | e |
| | 6 | 2 | f | |
+-+
6 rows in set (0.00 sec)
Mysql > select * from a where 2 > (select count (*) from a b where a.fid=b.fid and a.id select * from a where 2 > (select count (*) from a b where a.fid=b.fid and a.id > b.id) order by fid
+-+
| | id | fid | name | |
+-+
| | 1 | 1 | a |
| | 2 | 1 | b | |
| | 4 | 2 | d | |
| | 5 | 2 | e |
+-+
4 rows in set (0.00 sec)
At this point, I believe you have a deeper understanding of "how to achieve grouping limit in SQL". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!