Variable en la consulta

UPDATE ost_timeblock 
SET 
  timeblock_due_date=DATE(timeblock_next_update),
  timeblock_next_update=DATE_ADD(NOW(),INTERVAL timeblock_recurrence_time **timeblock_recurrence_unit** )
WHERE timeblock_recurrence=1 
  AND timeblock_complete=0 
  AND timeblock_next_update=FROM_UNIXTIME(1337662800)

when i use manual day or month as timeblock_recurrence_unit funciona bien.

timeblock_recurrence_unit = enum(day,month,year) i want value of timeblock_recurrence_unit at above bold location

Is there any way to do it with cases.

If timeblock_recurrence_unit = 'day' then 
timeblock_next_update=DATE_ADD(NOW(),INTERVAL timeblock_recurrence_time day )

If timeblock_recurrence_unit = 'month' then  
timeblock_next_update=DATE_ADD(NOW(),INTERVAL timeblock_recurrence_time month)

preguntado el 22 de mayo de 12 a las 20:05

please format your query that it fits in several lines -

I refer the honourable gentleman to este. Unfortunately, it's just not possible. Sorry. -

2 Respuestas

UPDATE ost_timeblock SET 
    timeblock_due_date = DATE(timeblock_next_update),
    timeblock_next_update = DATE_ADD(NOW(), INTERVAL timeblock_recurrence_time      
**timeblock_recurrence_unit** )
WHERE 
    timeblock_recurrence=1 
AND timeblock_complete=0 
AND timeblock_next_update = FROM_UNIXTIME(1337662800)

According to mysql doc: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add

The date_add feature accepts an inverval of dates and a unit, to perform dates arithmetic. You have to provide a given unit, not an ENUM of units like you would like to do.

You will have to revise your functional requirements and maybe think again whether the DATE_ADD method is performing the wanted action.

rgds

contestado el 22 de mayo de 12 a las 20:05

You could use user define variable as follows; See http://dev.mysql.com/doc/refman/5.0/en/user-variables.html

SET @timeblock_recurrence_unit = 'DAY';
UPDATE ost_timeblock SET
 timeblock_due_date=DATE(timeblock_next_update),
 timeblock_next_update=DATE_ADD(
       NOW(),
       INTERVAL timeblock_recurrence_time @timeblock_recurrence_unit
 ) WHERE 
    timeblock_recurrence=1 AND 
    timeblock_complete=0 AND
    timeblock_next_update= FROM_UNIXTIME(1337662800);

contestado el 22 de mayo de 12 a las 20:05

@user1410812, see the comment by DaveRandom above. That simply won't work, you can't use a variable for the unit, it has to be a constant. - bfavaretto

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.