MySQL數據中每隔幾行取一條數據


select * from (select @n:=@n+1 as n, a.* from (select * from tablename order by id desc)a,(select @n:=0)b)c where c.n%2=1 limit 10

首先 :select @n:=@n+1 as n, a.* from (select * from tablename order by id desc)a,(select @n:=0)b 是給每行的記錄添加了序號,字段名為n。

其次:where c.n%2=1 是對2取模值也就是取余數,2代表着隔一行取一條數據,如果是隔兩行取一條數據,就是where c.n%3=1,以此類推。

最后:limit 10 是限制條數為10條。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM