Мой новый хостинг-провайдер работает под управлением 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
Так в чем же проблема? Я в тупике.
'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
Если между выражением заменить на> =, <=
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)
DATE
илиTIMESTAMP
. Например,timestamp '2018-01-01 00:00:00'
илиdate '2018-01-01'
.BETWEEN
не рекомендуется из-за временной точности. Используйтеcase when now() >= date '2018-01-01' and now() < date '2018-07-01' then
вместо этого. Или используйте даты без частей времени:case when current_date between date '2018-01-01' and date '2018-06-30' then
.