Count or Account in a SELECT MySQL

1

I would like to make a query with a column that lists the results but to a certain extent and then restart the account: For example:

The query lists my 8 results .. in this case, name.

  • Carolina
  • Claudia
  • Daniel
  • Francisco
  • Jose
  • Maria
  • Mirta
  • Romina
  • But what I want is for you to count 4 records and start over. Example:

  • Carolina
  • Claudia
  • Daniel
  • Francisco
  • and there we restart ...

  • Jose
  • Maria
  • Mirta
  • Romina
  • Could it be ???

    I thought so, to list them all, but I do not know how to stop at 4 and start again in the same query.

    SELECT apellido,nombre, CAST(@s:=@s+1 AS UNSIGNED) AS '#' 
    FROM mitabla,
    (SELECT @s:= 0) AS s
    WHERE   ciudad = "NEUQUEN" and ORDER BY apellido asc, nombre asc 
    
        
    asked by Nano de los Santos 23.10.2018 в 18:44
    source

    1 answer

    1

    You've almost solved it:

    SELECT apellido,
           nombre, 
           CAST(@s:= (CASE WHEN @s = 4 THEN 1 ELSE @s+1 END) AS UNSIGNED) AS '#' 
           FROM mitabla,
           (SELECT @s:= 0) AS s
           WHERE   ciudad = "NEUQUEN" 
           ORDER BY apellido asc, nombre asc
    

    With (CASE WHEN @s = 4 THEN 1 ELSE @s+1 END) we reset the counter every time we reach 4.

        
    answered by 23.10.2018 / 21:11
    source