我有一个表名视频
表视频
id name upload_date
1 one 1408336348
2 two 1409884215
现在我想要选择所有数据,还要计算过去2天之间上传的视频,然后是是或否
结果如
id name upload_date new
1 one 1408336348 no
2 two 1409884215 yes
我正在使用此查询但无法正常工作
SELECT v.*,( if(from_unixtime(v.upload_date) < NOW() - INTERVAL 2 DAY) then 'no' else 'yes')
AS new FROM `video` as v
查询返回错误
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') then 'no' else 'yes')
AS new FROM `video` as v
LIMIT 0, 25' at line 1
答案 0 :(得分:1)
这不是MySQL中IF
的正确语法。试试这样:
SELECT v.*,
IF(from_unixtime(v.upload_date) < NOW() - INTERVAL 2 DAY, 'yes', 'no') AS new
FROM `video` as v
答案 1 :(得分:0)
尝试CASE声明:
SELECT v.*,
CASE WHEN from_unixtime(v.upload_date) < NOW() - INTERVAL 2 DAY
THEN 'Yes'
ELSE 'No'
END AS new
FROM `video`
这是在SQL中执行此操作的ansi标准方法。
答案 2 :(得分:0)
尝试使用案例
SELECT v.*
,case
when (from_unixtime(v.upload_date) < NOW() - INTERVAL 2 DAY) then 'yes'
else 'no')
end AS new
FROM `video` as v