How to output every 3 records from a table in MySQL? Which operators to use for this

  • the simplest is the remainder of dividing id by 3. but it is necessary that id should go in a row. or renumber the variable records and for it as well. In general, the task is strange, I wonder why it might be needed - Mike
  • I practice and it became interesting how to implement it. Only about the remainder of the division I have not heard before. - A.Dav
  • I know what is the remainder of the division, but I did not know about the implementation on Sql, thanks. - A.Dav

1 answer 1

A simple way, but based on the assumption that id go in a row:

select * from table_name where id % 3 = 1; 

More difficult, but not making assumptions:

 SELECT * FROM ( SELECT @row := @row + 1 AS rownum, some_column FROM (SELECT @row := 0) r, table_name ) ranked WHERE rownum % 3 = 1 
  • one
    It should also be noted that the return order is not guaranteed and add the ORDER BY ID. - Smithson