【问题标题】:Laravel SQL Query Keeps Giving SQLSTATE[42000]: Syntax error or access violation: 1064Laravel SQL 查询不断给出 SQLSTATE[42000]:语法错误或访问冲突:1064
【发布时间】:2017-02-25 15:31:35
【问题描述】:

我在 laravel 的控制器中有这个 SQL 查询

$distinct_course = DB::table('student')
    ->select(DB::raw('count(*) as grad_count, `student_course`, MONTH(`student_date_ended`)'))
    ->where('student_course', '=', 'Basic Computer')
    ->whereYear('student_date_ended', '=', '2015')
    ->groupby(DB::raw('MONTH(`student_date_ended`'))
    ->get(); 

这是基于我在将其转换为 Laravel 之前首先制作的这个 SQL 查询

select count(*) as grad_count, `student_course`, MONTH(`student_date_ended`) from `student` where `student_course` = "Basic Computer" and year(`student_date_ended`) = 2015 group by MONTH(`student_date_ended`)

但由于某种原因,我总是收到此错误。

SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的 '' 附近使用正确的语法(SQL:select count(*) as grad_count, student_course, MONTH(student_date_ended) from student where @987654326 @ = 基本计算机和年份(student_date_ended) = 2015 年按月分组(student_date_ended)

我在这里做错了我不知道的事情吗?

【问题讨论】:

    标签: php sql database laravel eloquent


    【解决方案1】:

    作为替代方案,在我的所有项目中,我都使用以下数据库交互方式:

    $Query = 'select count(*) as grad_count, `student_course`, 
      MONTH(`student_date_ended`) 
      from `student` 
      where `student_course` = ? 
      and year(`student_date_ended`) = ? 
      group by MONTH(`student_date_ended`)';
    
    $GradStudents = DB::select($Query, [ $StudenCourse, $StudentDateEnded]);
    

    这样我可以从确认的工作 MySQL 语句转到 Laravel,而不必担心转换后调用不起作用。

    注意:我不会将您的数据库调用保存在控制器中,而是将它们保存到模型文件夹中。这样您就可以维护 MVC 设计模式。

    【讨论】:

    • 非常感谢!我还在习惯 Laravel,所以我仍然需要学习如何在我的项目中使用模型。
    • @Paul:我试过你的方案,但我得到了SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 如果我使用 group by,我无法在 select('') 中选择多个值,知道如何解决它??
    【解决方案2】:

    在 Paul 的帮助下,我设法让它与这段代码一起工作。

    $Query = 'select count(*) as grad_count, `student_course`, 
                  MONTH(`student_date_ended`) as Month 
                  from `student` 
                  where `student_course` = ? 
                  and year(`student_date_ended`) = ? 
                  group by MONTH(`student_date_ended`), student_course';
    
      $StudentCourse = 'Basic Computer';
      $StudentDateEnded = 2015; 
    
      $distinct_course = DB::select($Query, [ $StudentCourse, $StudentDateEnded]);
    

    问题的一部分似乎是我还需要在 group by 中添加“student_course”列,所以我尝试将它添加到我原来的 Laravel 代码中并设法让它工作用这种方式。

     $distinct_course = DB::table('student')
        ->where('student_course', '=', 'Basic Computer')
        ->whereYear('student_date_ended', '=', '2015')
         ->select(DB::raw('count(*) as grad_count, `student_course`, MONTH(`student_date_ended`) as Month'))
        ->groupby(DB::raw('MONTH(`student_date_ended`), student_course'))
        ->get(); 
    

    【讨论】:

      猜你喜欢
      • 2017-10-29
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2014-01-14
      • 2013-12-02
      • 1970-01-01
      相关资源
      最近更新 更多