【问题标题】:Laravel- Check the manual that corresponds to your MariaDB server version for the right syntax to use near ') < Now()' at line 1Laravel-检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的 ') < Now()' 附近使用正确的语法
【发布时间】:2021-09-27 00:42:13
【问题描述】:

我在使用 Laravel 执行程序时不断收到此错误:

SQLSTATE[42000]: Syntax error or access violation: 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 ') < Now()' at line 1 (SQL: select count(*) as aggregate from `house` where `isoccupied` = 0 and (currentdatasync + INTERVAL '1 DAY') < Now())

我在控制器中的功能是:

  public function view()
    {
        $failed = DB::table('house')
            ->where('isoccupied', 0)
            ->whereRaw("(currentdatasync + INTERVAL '1 DAY') < Now()")
            ->count('*');

        return view('admin.dashboard')->with('failed', $failed);
    }

我试过用

return view('admin.dashboard')->with(compact($failed));

但是还是不行。

以前有人经历过吗?

【问题讨论】:

  • 不要引用持续时间 + INTERVAL 1 DAY 应该可以正常工作

标签: php sql laravel laravel-5


【解决方案1】:

您不必使用+ INTERVAL '1 DAY'

尝试使用

public function view()
    {
        $failed = DB::table('house')
            ->where('isoccupied', 0)
            ->whereRaw("subdate(currentdatasync, 1) < Now()")
            ->count('*');

        return view('admin.dashboard')->with('failed', $failed);
    }

或将subdate(currentdatasync, 1) 替换为subdate(currentdatasync, -1),具体取决于您是昨天还是明天需要(1 是昨天,-1 是明天)


更新

如果你想使用INTERVAL,可以这样使用:

public function view()
    {
        $failed = DB::table('house')
            ->where('isoccupied', 0)
            ->whereRaw("(`currentdatasync` + INTERVAL 1 DAY) < NOW()")
            ->count('*');

        return view('admin.dashboard')->with('failed', $failed);
    }

【讨论】:

  • 我不同意。 INTERVAL 语法是 SQL 标准语法,它使查询更具可移植性。
  • 我已经提供了完整的查询,请提供你自己的解决方案,除了评论其他人:) 如果你知道如何在这种情况下使用INTERVAL,我想学习一些新的东西。
猜你喜欢
  • 1970-01-01
  • 2019-05-31
  • 1970-01-01
  • 2015-09-23
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 1970-01-01
相关资源
最近更新 更多