MYSQL SQLSTATE [42000]:语法错误或访问冲突:1064

时间:2018-10-04 10:22:33

标签: mysql laravel innodb

我有代码,但是本地主机没有我无法理解的错误帮助 MySQL的5.7.21 INNODB 代码:

$sql = DB::select('
            SELECT 
                (case when (CAST(to as time) <= CAST("'.$time.'" as time) and 
                            CAST(do as time) >= CAST("'.$time.'" as time) or 
                            (to is null and do is null)) 
                             THEN
                                  1 
                             ELSE
                                  0 
                             END) as working
            FROM 
                franchises
    ');

错误:

SQLSTATE[42000]: Syntax error or access violation: 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 'to as time) <= 
CAST("10:15" as time) and CAST(do as time) >= CAST("10:15" as tim' at
line 2 (SQL:

1 个答案:

答案 0 :(得分:3)

To是MySQL中的reserved word,因此您需要将其包括在反引号中:

...case when (CAST(`to` as time)...