Mysql
 sql >> Database >  >> RDS >> Mysql

MySQL Order By valori alternativi

Ecco un'opzione che utilizza user-defined variables . Fondamentalmente crea un Row Number per gruppo, quindi ordina in base a esso insieme a rec_f_id campo:

SELECT rec_id, rec_f_id
FROM (
  SELECT rec_id, rec_f_id,
    @rn:=IF(@prev=rec_f_id,@rn+1,1) rn,
    @prev:=rec_f_id
  FROM tbl_records
    JOIN (SELECT @rn:=0, @prev:=0) t
  ORDER BY rec_f_id
  ) t
ORDER BY rn, rec_f_id