我有sql查询,如果2次之间的差异大于1440,那么它将除以7,否则它将返回实际值。问题是我在重复值中重复代码。
SELECT
'IF(TIMESTAMPDIFF(MINUTE, now(), end_at) > 1440, TIMESTAMPDIFF(MINUTE, now(), end_at)/ 7, TIMESTAMPDIFF(MINUTE, now(), end_at)) as minutesLeft'
FROM table
如何将重复代码替换为这样?
SELECT
'IF(TIMESTAMPDIFF(MINUTE, now(), end_at) > 1440, timestamp / 7, timestamp ) as minutesLeft'
FROM table
答案 0 :(得分:1)
您可以尝试使用语句内分配:
set @diff = null;
SELECT @diff := TIMESTAMPDIFF(MINUTE, now(), end_at),
IF( @diff > 1440, @diff / 7, @diff) as minutesLeft
FROM table
如果您只想返回一列,请在子查询中包装选择。