自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

背景

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

復(fù)現(xiàn)

創(chuàng)建一個(gè)簡(jiǎn)單表,并插入一些數(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 秒) 

 

分頁(yè)的寫法

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

limit分頁(yè)公式

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

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

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ù),這時(shí)可以加入第二個(gè)排序字段,提高排序的唯一性,

 

  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)我們加上了第二個(gè)排序字段時(shí)分頁(yè)數(shù)據(jù)變得正常了。

總結(jié)

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

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

2010-10-27 15:40:14

oracle分頁(yè)查詢

2020-03-12 13:58:19

MySQL分頁(yè)數(shù)據(jù)庫(kù)

2011-04-29 13:23:11

分頁(yè)數(shù)據(jù)存儲(chǔ)

2009-07-03 14:23:49

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

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分頁(yè)

2015-07-16 17:13:13

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

2022-06-06 11:31:31

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

2009-08-04 14:23:36

ASP.NET查詢分頁(yè)

2023-03-13 07:41:34

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

2024-09-22 14:17:54

2010-11-25 14:21:16

MySQL查詢分頁(yè)

2009-07-01 10:01:33

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

2010-09-06 11:40:06

SqlServer語(yǔ)句

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ù)庫(kù)查詢查詢性能分頁(yè)瀏覽
點(diǎn)贊
收藏

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