繁体   English   中英

Laravel DB::raw 返回 - SQLSTATE[42000]:语法错误或访问冲突

[英]Laravel DB::raw returning - SQLSTATE[42000]: Syntax error or access violation

为什么我会收到这个错误? 当我在 phpMyAdmin 中运行它时,SQL 工作正常。

SQLSTATE[42000]:语法错误或访问冲突:1064 你的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行附近使用的正确语法(SQL:select id from users where active = true andbirthyear != 0000 and not exist(select id from agestats where users.id = agestats.user_id 和 agestats.year = 2020-01-13)

查询语句:

select id
from users
where active = true
  and birthyear != 0000
  and not exists (
    select 1
    from agestats
    where users.id = agestats.user_id
      and agestats.year = 2020
  )

和 Laravel 代码:

$membersToInsert = DB::select(DB::raw(
  'select id
  from users
  where active = true
    and birthyear != 0000
    and not exists (
      select id
      from agestats
      where users.id = agestats.user_id
        and agestats.year = ' . date('Y-m-d')
));

date('Ym-d')会给你当前日期,比如2020-01-13所以要得到2020然后使用date('Y') 我希望这对你的情况有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM