查询中的变量

时间:2012-05-22 19:19:22

标签: php mysql database

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)

当我使用手动日期或月份timeblock_recurrence_unit时,它可以正常工作。

timeblock_recurrence_unit = enum(day,month,year)我希望timeblock_recurrence_unit的价值高于大胆的位置

有没有办法处理案件。

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)

2 个答案:

答案 0 :(得分:0)

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)

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

date_add功能接受日期和单位的inverval,以执行日期算术。 你必须提供一个给定的单位,而不是你想要的ENUM单位。

您必须修改您的功能需求,并且可能会再次考虑DATE_ADD方法是否正在执行所需操作。

RGDS。

答案 1 :(得分:0)

您可以使用用户定义变量,如下所示; 见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);