SQL實現分組limit
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,'a');
Query OK, 1 row affected (0.04 sec)
mysql> insert into a value (2,1,'b');
Query OK, 1 row affected (0.01 sec)
mysql> insert into a value (3,1,'c');
Query OK, 1 row affected (0.01 sec)
mysql> insert into a value (4,2,'d');
Query OK, 1 row affected (0.02 sec)
mysql> insert into a value (5,2,'e');
Query OK, 1 row affected (0.01 sec)
mysql> insert into a value (6,2,'f');
Query OK, 1 row affected (0.00 sec)
+------+------+------+
| id | fid | name |
+------+------+------+
| 2 | 1 | b |
| 3 | 1 | c |
| 5 | 2 | e |
| 6 | 2 | f |
+------+------+------+
4 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>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)
Query OK, 0 rows affected (0.10 sec)
mysql> insert into a value (1,1,'a');
Query OK, 1 row affected (0.04 sec)
mysql> insert into a value (2,1,'b');
Query OK, 1 row affected (0.01 sec)
mysql> insert into a value (3,1,'c');
Query OK, 1 row affected (0.01 sec)
mysql> insert into a value (4,2,'d');
Query OK, 1 row affected (0.02 sec)
mysql> insert into a value (5,2,'e');
Query OK, 1 row affected (0.01 sec)
mysql> insert into a value (6,2,'f');
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<b.id )order by fid ;-> ;
+------+------+------+
| 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)
+------+------+------+
| id | fid | name |
+------+------+------+
| 2 | 1 | b |
| 3 | 1 | c |
| 5 | 2 | e |
| 6 | 2 | f |
+------+------+------+
4 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>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)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28282660/viewspace-2133644/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 如何在SQL Server中實現 Limit m,n 的功能SQLServerMIT
- MySQL實現分組排序MySql排序
- SQL-分組聚合 - 單欄位分組SQL
- SQL之limit子句的使用SQLMIT
- SQL優化之limit 1SQL優化MIT
- sql中limit使用方法SQLMIT
- mysql分頁-limit offset分頁MySqlMIT
- Java利用Comparator實現分組排序Java排序
- mysql 使用技巧 分頁limitMySqlMIT
- SQL 如何查詢每個分組都出現的欄位值SQL
- 偷樑換柱 - iOS實現UITextField+LimitiOSUIMIT
- Mysql 分組排序的sql寫法MySql排序
- MongoDB 如何實現巢狀子文件分組MongoDB巢狀
- vxe-table 實現表格資料分組,按指定欄位資料分組
- 用listagg函式分組實現列轉行函式
- sql改寫優化:簡單規則重組實現SQL優化
- dubbo-go 中的 TPS Limit 設計與實現GoMIT
- 編寫一個 SQL 查詢來實現分數排名。SQL
- sql 按著時間分組,每組取 20 條資料SQL
- django實現分頁的模組(匯入即可用)Django
- Python正規表示式實現非捕獲分組Python
- 【Gin-API系列】實現動態路由分組(七)API路由
- Laravel-admin 實現一個分組求和的資料展現Laravel
- MaxCompute如何對SQL查詢結果實現分頁獲取SQL
- 查詢統計SQL分組求和使用小技巧SQL
- AWR TOP SQL實現SQL
- Oracle總結【SQL細節、多表查詢、分組查詢、分頁】OracleSQL
- 如何在分組報表中實現組內資料補空行及組內頁碼
- BIRT 中根據引數實現動態日期分組
- SQL 如何計算每個分組的中位數SQL
- 使用 CSS columns 佈局來實現自動分組佈局CSS
- 一句話實現MySQL庫中的重疊分組MySql
- MySQL order by limit 分頁資料重複問題MySqlMIT
- Lumen 實現 SQL 監聽SQL
- 用pandas實現SQL功能SQL
- sql中的left切割 與 group by後的兩次分組SQL
- 基於粒子群演算法的分組揹包MATLAB實現演算法Matlab
- Android 從零開始實現RecyclerView分組及粘性頭部效果AndroidView
- 淺析Oracle(rownum)和Mysql(limit)分頁的區別OracleMySqlMIT