MYSQL:在旧版本中使用带有CASE的FROM_UNIXTIME(0)的错误?

时间:2018-03-23 14:07:46

标签: mysql

我正在尝试使用MySQL客户端版本中的CASE语句格式化我的日期:5.1.41。 examine_on int(11)default 0用于存储epoch / unix时间戳。而不是得到0或dd / mm / yyyy,结果在前面加上真实结果3,即如果真实结果为0则返回30;如果它是10然后它返回3130 ...

我检查了各种链接以及SQL case when FROM(UNIX_TIME)

@Martin Orem的结论是升级sql版本。它是唯一的解决方案还是我们仍然可以解决这个问题并获得所需的输出?任何帮助将不胜感激。

mysql>SELECT reviewed_on AS reviewed_on FROM table_name
+----------------------+
| reviewed_on          |
+----------------------+
| 0                    |
+----------------------+

mysql>SELECT FROM_UNIXTIME(reviewed_on) AS reviewed_on FROM table_name
+----------------------+
| reviewed_on          |
+----------------------+
| 1970-01-01 01:00:00  |
+----------------------+

mysql>SELECT CASE reviewed_on WHEN 0 THEN 0 ELSE 1 END AS reviewed_on
FROM table_name
+----------------------+
| reviewed_on          |
+----------------------+
| 0                    |
+----------------------+

mysql>SELECT CASE reviewed_on WHEN 0 THEN 0 ELSE FROM_UNIXTIME(reviewed_on, '%d/%m/%Y') END AS reviewed_on
FROM table_name
+----------------------+
| reviewed_on          |
+----------------------+
| 30                   |
+----------------------+

mysql>SELECT CASE reviewed_on WHEN 0 THEN 10 ELSE DATE_FORMAT(FROM_UNIXTIME(reviewed_on), '%d/%m/%Y') END AS reviewed_on
FROM table_name
+----------------------+
| reviewed_on          |
+----------------------+
| 3130                 |
+----------------------+

1 个答案:

答案 0 :(得分:0)

由于没有人回答这个问题。我做了一些解决方法并将列reviewed_on更改为type date DEFAULT NULL。它有效。

mysql>SELECT IFNULL(DATE_FORMAT(reviewed_on, '%d/%m/%Y'), '0') AS reviewed_on FROM table_name
+----------------------+
| reviewed_on          |
+----------------------+
| 27/03/2018           |
+----------------------+
| 0                    |
+----------------------+