0

Estoy creando un secuencial el cual este conformado por la fecha actual y un dígito incrementable, por ejemplo

20190226_001
20190226_002
20190226_003 ...
20190227_001
20190227_002 ...

pero aun no logro hacer que funcione correctamente .

select  (concat(date_format(now(), '%Y%m%d'), LPAD((coalesce((MAX(substr(i_ticket,10,12 ))),0))+1 ,4,'_00'  ))) AS i_ticket
 from   ticket_support
WHERE i_ticket like ('20190227%') and  d_request= date_format(now(), '%Y%m%d'); 

2 respuestas 2

0

Podría ser algo así:

SELECT CONCAT(DATE_FORMAT(NOW(), '%Y%m%d'),'_',LPAD( @a:=@a+1,4,'0') ) AS i_ticket
  FROM ticket_support
 WHERE i_ticket like ('20190227%') 
   AND d_request = DATE_FORMAT(now(), '%Y%m%d')

Lo que se hace aquí es ayudarse de una variable de usuario, que se le incrementa en cada iteración, para ser usada como contador.

9.4 User-Defined Variables

You can store a value in a user-defined variable in one statement and refer to it later in another statement. This enables you to pass values from one statement to another.

User variables are written as @var_name, where the variable name var_name consists of alphanumeric characters, ., _, and $. A user variable name can contain other characters if you quote it as a string or identifier (for example, @'my-var', @"my-var", or @my-var).

User-defined variables are session specific. A user variable defined by one client cannot be seen or used by other clients. (Exception: A user with access to the Performance Schema user_variables_by_thread table can see all user variables for all sessions.) All variables for a given client session are automatically freed when that client exits.

User variable names are not case-sensitive. Names have a maximum length of 64 characters.

Fuente: https://dev.mysql.com/doc/refman/8.0/en/user-variables.html


ATENCIÓN

No se reiniciará el contador si en vez de now() usas un campo fecha y este varía.

0

Hola muchas gracias por la ayuda, en efecto no se reiniciaba porque tenia una columna de mas en el filtro where, la solucion final para mi fue:

select (concat(date_format(now(), '%Y%m%d'), LPAD((coalesce((MAX(substr(i_ticket,10,12 ))),0))+1 ,4,'_00' ))) AS i_ticket from ticket_support WHERE i_ticket like date_format(now(), '%Y%m%d%');

Gracias!

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.