MariaDB查看不起作用但声明

时间:2018-01-17 11:04:22

标签: mysql sql mariadb sql-view sqldatetime

我的新托管服务提供商正在运行MySQL版本10.0.31-MariaDB-cll-lve

我有一个在MySQL 5.6中工作正常但在MariaDB中不起作用的视图。

我创建了一个简单的缩减版本,只是为了显示导致错误的原因。

我可以创建视图,但我无法使用它:

CREATE VIEW `test_date` 
AS select (case 
   when (now() between '2018-01-01 00:00:00' and '2018-06-30 23:59:59') 
     then '2018-06-30' 
   else NULL end) - interval 4 month

尝试打开时出现的错误:

#1064 - You have an error in your SQL syntax; check the manual that 
corresponds to your MariaDB server version for the right syntax to use 
near '21:05:05 between 2018-01-01 00:00:00 and 2018-06-30 23:59:59) 
then '2018-06-30' ' at line 1

我无法看到任何错误,并且在普通的MySQL服务器上运行正常。

我尝试删除了4个月的间隔时间'它工作正常:

CREATE VIEW `test_date` 
AS select case 
  when (now() between '2018-01-01 00:00:00' and '2018-06-30 23:59:59') 
    then '2018-06-30' 
  else NULL end

我尝试用简单的数字替换日期,并且工作正常:

CREATE VIEW `test_date` 
AS select (case 
   when (3 between 1 and 5) 
     then '2018-06-30' 
   else NULL end) - interval 4 month

那么这里真正的问题是什么?我很难过。

1 个答案:

答案 0 :(得分:2)

'2018-06-30'并没有隐式转换为日期(我猜这是mysql版本之间收紧的东西之一,或者是mariadb的分支)尝试显式转换它。

drop view if exists test_date;
CREATE VIEW `test_date` AS 
select (case when (now() between '2018-01-01 00:00:00' and '2018-06-30 23:59:59') then str_to_date('2018-06-30','%Y-%m-%d') else NULL end) 
- interval 4 month;

select * from test_date;

+------------+
| Name_exp_1 |
+------------+
| 2018-02-28 |
+------------+
1 row in set (0.00 sec)

奇怪的是,单独的选择工作正常,只有在视图中使用时(可能与之间的语句一起使用)它才不会

MariaDB [sandbox]> select (case
    ->    when (now() between '2018-01-01 00:00:00' and '2018-06-30 23:59:59')
    ->      then '2018-06-30'
    ->    else NULL end) - interval 4 month rd;
+------------+
| rd         |
+------------+
| 2018-02-28 |
+------------+
1 row in set (0.00 sec)

在视图中使用时会抛出错误

MariaDB [sandbox]> create view test_date as
    -> select
    -> (
    -> case when (now() between '2018-01-01 00:00:00' and '2018-06-30 23:59:59') then '2018-06-30'
    -> else NULL
    -> end
    -> ) - interval 4 month as rd
    -> ;
Query OK, 0 rows affected (0.04 sec)

MariaDB [sandbox]> select rd from test_date;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '08:16:57 between 2018-01-01 00:00:00 and 2018-06-30 23:59:59) then '2018-06-30' ' at line 1

如果between语句被> =,< =

替换
MariaDB [sandbox]> create view test_date as
    -> select
    -> (
    -> case when (now() >= '2018-01-01 00:00:00' and now() <= '2018-06-30 23:59:59') then '2018-06-30'
    -> else NULL
    -> end
    -> ) - interval 4 month as rd
    -> ;
Query OK, 0 rows affected (0.04 sec)

MariaDB [sandbox]> select rd from test_date;
+------------+
| rd         |
+------------+
| 2018-02-28 |
+------------+
1 row in set (0.00 sec)