Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
¿Interpretar la columna MySQL como una unidad de intervalo de fecha?

Al usar funciones de fecha y hora en MySQL, como DATE_ADD , ¿es posible almacenar la unidad INTERVALO en una columna y usarla en una consulta?

Por ejemplo, considere la siguiente tabla:

 +---------------------------+---------------------------------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------------------+---------------------------------------------------+------+-----+---------+----------------+ | snapshotSchedule_entry_id | int(10) unsigned | NO | PRI | NULL | auto_increment | | snapshotSchedule_id | int(10) unsigned | NO | PRI | NULL | | | everyFrequency | smallint(5) unsigned | NO | | NULL | | | everyInterval | enum('minute','hour','day','week','month','year') | NO | | NULL | | | afterFrequency | smallint(5) unsigned | NO | | NULL | | | afterInterval | enum('minute','hour','day','week','month','year') | NO | | NULL | | +---------------------------+---------------------------------------------------+------+-----+---------+----------------+

La siguiente consulta funciona bien, porque solo trata con la expresión INTERVALO:

 select * from snapshotSchedule_entry order by date_add(now(), interval everyFrequency month);

Cuando quiero tratar con la unidad de intervalo (por ejemplo, mes, año, etc.), no lo interpreta.

mysql> select * from snapshotSchedule_entry order by date_add(now(), interval 1 everyInterval);ERROR 1064 (42000): Tiene un error en su sintaxis SQL; consulte el manual que corresponde a la versión de su servidor MySQL para conocer la sintaxis correcta para usar cerca de 'everyInterval)' en la línea 1

No me sorprende este comportamiento, solo tengo curiosidad por saber si es posible hacer que esto funcione. No es el fin del mundo hacer esto en código, pero me pregunto si es posible.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

MySQL no es tan flexible con las expresiones de intervalo. Necesitaría una expresión de case grande con valores codificados:

 order by case everyInterval when 'minute' then now() + inteval everyFrequency minute when 'hour' then now() + inteval everyFrequency hour when 'day' then now() + inteval everyFrequency day when 'week' then now() + inteval everyFrequency week when 'month' then now() + inteval everyFrequency month when 'year' then now() + inteval everyFrequency year end

Una alternativa más sencilla sería rediseñar su esquema para almacenar el intervalo con frecuencias fijas, digamos segundos y meses. Entonces podrías hacer:

 order by now() + interval everyFrequencySecond second + interval everyFrequencyMonth month
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!