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

Php Laravel DB::raw returning-SQLSTATE[42000]:语法错误或访问冲突,php,mysql,laravel,Php,Mysql,Laravel,为什么会出现这个错误? 当我在phpMyAdmin中运行SQL时,它工作得很好 SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以了解第1行附近要使用的正确语法SQL:select id from users,其中active=true和birthyear!=0000且不存在从agestats中选择id,其中users.id=agestats.user_id和agestats.year=2020-01-13 SQL: 和

为什么会出现这个错误? 当我在phpMyAdmin中运行SQL时,它工作得很好

SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以了解第1行附近要使用的正确语法SQL:select id from users,其中active=true和birthyear!=0000且不存在从agestats中选择id,其中users.id=agestats.user_id和agestats.year=2020-01-13

SQL:

和拉威尔代码:

$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')
));

日期“Y-m-d”将给出当前日期,如2020-01-13,因此要得到2020,请使用日期“Y”。我希望这对你的情况有所帮助。换行和缩进是你的朋友,尤其是当你要求别人阅读你的代码时。2.日期“Y-m-d”将是2020-01-13,而不是2020年。3.需要引用日期字符串。4.我刚才介绍的换行符会使错误信息更加相关。哎哟,我自己应该看到的。我很确定这些代码以前一直在工作。当然不可能。非常感谢。也谢谢你给我一个关于正确写作的提示。我会更好,如果有更多的职位。再次感谢你!工作完美!谢谢
$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')
));