偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

MySQL排序后分頁,因數(shù)據(jù)重復(fù)導(dǎo)致分頁數(shù)據(jù)紊亂的問題

數(shù)據(jù)庫 MySQL
前不久在寫一個分頁接口的時候,在測試階段出現(xiàn)了排序結(jié)果紊亂且數(shù)據(jù)不正確的問題,那個接口是按照create_time進(jìn)行排序的,但是對應(yīng)的表中有很多相同create_time的數(shù)據(jù),最后發(fā)現(xiàn)是因為 order by 排序的時候,如果排序字段中有多行相同的列值,則排序結(jié)果是不確定的。

背景

前不久在寫一個分頁接口的時候,在測試階段出現(xiàn)了排序結(jié)果紊亂且數(shù)據(jù)不正確的問題,那個接口是按照create_time進(jìn)行排序的,但是對應(yīng)的表中有很多相同create_time的數(shù)據(jù),最后發(fā)現(xiàn)是因為 order by 排序的時候,如果排序字段中有多行相同的列值,則排序結(jié)果是不確定的。

復(fù)現(xiàn)

創(chuàng)建一個簡單表,并插入一些數(shù)據(jù)

 

  1. mysql> desc people; 
  2. +-------------+-------------+------+-----+---------+----------------+ 
  3. | Field       | Type        | Null | Key | Default | Extra          | 
  4. +-------------+-------------+------+-----+---------+----------------+ 
  5. | id          | bigint(20)  | NO   | PRI | NULL    | auto_increment | 
  6. name        | varchar(20) | NO   |     | NULL    |                | 
  7. | create_time | bigint(20)  | NO   |     | NULL    |                | 
  8. +-------------+-------------+------+-----+---------+----------------+ 
  9. 3 行于數(shù)據(jù)集 (0.02 秒) 
  10.  
  11. mysql> select * from people; 
  12. +----+--------+-------------+ 
  13. | id | name   | create_time | 
  14. +----+--------+-------------+ 
  15. | 1  | 張三 | 1           | 
  16. | 2  | 李四 | 2           | 
  17. | 3  | 王五 | 3           | 
  18. | 4  | 趙六 | 4           | 
  19. | 5  | 孫七 | 2           | 
  20. | 6  | 趙八 | 2           | 
  21. | 7  | 吳九 | 2           | 
  22. | 8  | 鄭十 | 2           | 
  23. +----+--------+-------------+ 
  24. 8 行于數(shù)據(jù)集 (0.02 秒) 

 

分頁的寫法

分頁一般有2個參數(shù):page:表示第幾頁,從1開始,范圍[1,+∞)pageSize:每頁顯示多少條記錄,范圍[1,+∞)

limit分頁公式

(1)limit分頁公式:curPage是當(dāng)前第幾頁;pageSize是一頁多少條記錄

limit (curPage-1)*pageSize,pageSize(2)用的地方:sql語句中

select 列 from 表名 limit(curPage-1)*pageSize,pageSize;

查詢復(fù)現(xiàn)

 

  1. mysql> select * from people order by create_time asc limit 0,2; 
  2. +----+--------+-------------+ 
  3. | id | name   | create_time | 
  4. +----+--------+-------------+ 
  5. | 1  | 張三 | 1           | 
  6. | 2  | 李四 | 2           | 
  7. +----+--------+-------------+ 
  8. 2 行于數(shù)據(jù)集 (0.06 秒) 
  9.  
  10. mysql> select * from people order by create_time asc limit 2,2; 
  11. +----+--------+-------------+ 
  12. | id | name   | create_time | 
  13. +----+--------+-------------+ 
  14. | 8  | 鄭十 | 2           | 
  15. | 6  | 趙八 | 2           | 
  16. +----+--------+-------------+ 
  17. 2 行于數(shù)據(jù)集 (0.09 秒) 
  18.  
  19. mysql> select * from people order by create_time asc limit 4,2; 
  20. +----+--------+-------------+ 
  21. | id | name   | create_time | 
  22. +----+--------+-------------+ 
  23. | 6  | 趙八 | 2           | 
  24. | 7  | 吳九 | 2           | 
  25. +----+--------+-------------+ 
  26. 2 行于數(shù)據(jù)集 (0.04 秒) 
  27.  
  28. mysql> select * from people order by create_time asc limit 6,2; 
  29. +----+--------+-------------+ 
  30. | id | name   | create_time | 
  31. +----+--------+-------------+ 
  32. | 3  | 王五 | 3           | 
  33. | 4  | 趙六 | 4           | 
  34. +----+--------+-------------+ 
  35. 2 行于數(shù)據(jù)集 (0.05 秒) 

 

排序字段出現(xiàn)重復(fù)數(shù)據(jù),這時可以加入第二個排序字段,提高排序的唯一性,

 

  1. mysql> select * from people order by create_time asc,id asc limit 0,2; 
  2. +----+--------+-------------+ 
  3. | id | name   | create_time | 
  4. +----+--------+-------------+ 
  5. | 1  | 張三 | 1           | 
  6. | 2  | 李四 | 2           | 
  7. +----+--------+-------------+ 
  8. 2 行于數(shù)據(jù)集 (0.05 秒) 
  9.  
  10. mysql> select * from people order by create_time asc,id asc limit 2,2; 
  11. +----+--------+-------------+ 
  12. | id | name   | create_time | 
  13. +----+--------+-------------+ 
  14. | 5  | 孫七 | 2           | 
  15. | 6  | 趙八 | 2           | 
  16. +----+--------+-------------+ 
  17. 2 行于數(shù)據(jù)集 (0.10 秒) 
  18.  
  19. mysql> select * from people order by create_time asc,id asc limit 4,2; 
  20. +----+--------+-------------+ 
  21. | id | name   | create_time | 
  22. +----+--------+-------------+ 
  23. | 7  | 吳九 | 2           | 
  24. | 8  | 鄭十 | 2           | 
  25. +----+--------+-------------+ 
  26. 2 行于數(shù)據(jù)集 (0.05 秒) 
  27.  
  28. mysql> select * from people order by create_time asc,id asc limit 6,2; 
  29. +----+--------+-------------+ 
  30. | id | name   | create_time | 
  31. +----+--------+-------------+ 
  32. | 3  | 王五 | 3           | 
  33. | 4  | 趙六 | 4           | 
  34. +----+--------+-------------+ 
  35. 2 行于數(shù)據(jù)集 (0.03 秒) 

我們可以觀察到第一次的查詢中,缺少了‘孫七’的數(shù)據(jù)行,當(dāng)我們加上了第二個排序字段時分頁數(shù)據(jù)變得正常了。

總結(jié)

MySQL 使用 limit 進(jìn)行分頁時,可能會出現(xiàn)重復(fù)數(shù)據(jù),通過加入 order by 子句可以解決,但是需要注意的是,如果排序字段有相同值的情況下,由于排序字段數(shù)據(jù)重復(fù),可能會導(dǎo)致每次查詢排序后結(jié)果順序不同,分頁還是會出現(xiàn)重復(fù)數(shù)據(jù),這時可以加入第二個排序字段,提高排序的唯一性,最好保證排序的字段在表中的值是唯一的,這樣就可以少寫一個排序字段,增加查詢效率,因為 order by 后面有多個排序字段時,無法用到索引。

責(zé)任編輯:華軒 來源: 今日頭條
相關(guān)推薦

2010-10-27 15:40:14

oracle分頁查詢

2009-07-03 14:23:49

JSP數(shù)據(jù)分頁

2011-04-29 13:23:11

分頁數(shù)據(jù)存儲

2020-03-12 13:58:19

MySQL分頁數(shù)據(jù)庫

2009-07-27 16:37:55

DetailsView

2024-12-05 09:06:58

2009-03-04 13:32:28

排序SQLOracle

2011-05-18 14:49:53

MySQL分頁

2015-07-16 17:13:13

shell分頁讀取MySQL數(shù)據(jù)腳本

2022-06-06 11:31:31

MySQL數(shù)據(jù)查詢

2009-08-04 14:23:36

ASP.NET查詢分頁

2023-03-13 07:41:34

分頁查詢數(shù)據(jù)排序

2010-11-25 14:21:16

MySQL查詢分頁

2024-09-22 14:17:54

2009-07-01 10:01:33

JSP分頁查詢MySQL數(shù)據(jù)庫

2010-09-06 11:40:06

SqlServer語句

2024-07-25 09:15:39

2009-08-07 09:20:26

DataPager數(shù)據(jù)

2010-09-14 10:47:45

sql server存

2009-05-15 10:11:55

數(shù)據(jù)庫查詢查詢性能分頁瀏覽
點贊
收藏

51CTO技術(shù)棧公眾號