mysql的limit、order by和group by的用法
程序執(zhí)行會重復(fù)
用mysql很長時間,limit是分頁的一個好工具,
select * from table_a where num = 4?limit 1,10,
select * from table_a where num = 4?limit 10,10,
今天突然發(fā)現(xiàn)有些數(shù)據(jù)怎么也不會出來
也就是說第一頁的數(shù)據(jù)會重復(fù)顯示在第二頁,有些在數(shù)據(jù)庫的數(shù)據(jù)不會被查詢出來
這樣就造成了數(shù)據(jù)的缺失,如果用
select * from table_a where num = 4 order by num1 ASC?limit 1,10,
select * from table_a where num = 4 order by num1 ASC?limit 10,10,
可以解決這個問題。
于是開始有g(shù)roup by的問題
select * from table_a where num = 4 group by num1?order by num1 ASC?limit 1,10,
select * from table_a where num = 4 group by num1?order by num1 ASC?limit 10,10,
這樣又會出現(xiàn)數(shù)據(jù)缺失的問題
這時候只有增加排序的字段來處理這個問題
也就是
select * from table_a where num = 4 group by num1?order by num1, num2 ASC?limit 1,10,
select * from table_a where num = 4 group by num1?order by num1, num2?ASC?limit 10,10,
這樣也只是目前解決了這個問題,如果說相同的字段很多,那這個方法也會出現(xiàn)問題
Live together,or Die alone!轉(zhuǎn)載于:https://www.cnblogs.com/chxl800/p/6004639.html
總結(jié)
以上是生活随笔為你收集整理的mysql的limit、order by和group by的用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP基础班初学感悟
- 下一篇: linux cmake编译源码,linu