Laravel SQL 结果不同于 MySQL 查询

Laravel SQL Result Different Than MySQL Query

根据我的各种搜索,我怀疑他下面的问题源于我正在使用 GROUP BY。然而,none 我遇到的例子和我的一样简单,他们的解决方案最终成为我无法理解的东西。我希望你能帮我弄清楚这里出了什么问题?

我在我的应用程序中使用了几个连接查询。当我 运行 在 mysql 客户端 (SQLYog) 中远程连接到 homestead VM 的查询时 - 我得到了正确的结果。当我通过 SSH 进入 VM 并 运行 MySQL 和 运行 查询时,我得到了正确的结果。 但是,当在 Laravel/the 应用程序中查询 运行 时,结果不同,我不明白为什么!

查询:

SELECT monthly_contribution, COUNT(*) AS contributors FROM students
INNER JOIN student_bursaries ON students.id=student_bursaries.student_id
INNER JOIN student_bursary_statuses ON student_bursaries.id=student_bursary_statuses.student_bursary_id
LEFT JOIN student_medical_aids ON students.`id`=student_medical_aids.`student_id`
INNER JOIN bursary_administrator_student ON students.`id`=bursary_administrator_student.`student_id`
WHERE student_bursary_statuses.`status` IN (1,4,6,7) AND 
bursary_administrator_student.`bursary_administrator_id`=1
GROUP BY monthly_contribution

MySQL 结果:

+----------------------+--------------+
| monthly_contribution | contributors |
+----------------------+--------------+
|                50.00 |          151 |
|                 NULL |            7 |
|               150.00 |            4 |
|               100.00 |            1 |
|               250.00 |            3 |
+----------------------+--------------+

Laravel 结果:

+----------------------+--------------+
| monthly_contribution | contributors |
+----------------------+--------------+
|                50.00 |          141 |
|                 NULL |            2 |
|               150.00 |            4 |
|               100.00 |            1 |
|               250.00 |            2 |
+----------------------+--------------+

我尝试了 COUNT(monthly_contribution) 但 returns NULL 值的计数为 0 - 我需要那里的计数。而且,无论如何,它并没有解决其他人的错误计数,所以不是那样。

我确实使用了 Log::debug(DB::getQueryLog());它确实输出了相同的查询,所以似乎也不是问题:

Laravel代码:

    $medical_aids = DB::select('SELECT monthly_contribution, COUNT(*) AS contributors FROM students
                                        INNER JOIN student_bursaries ON students.id=student_bursaries.student_id
                                        INNER JOIN student_bursary_statuses ON student_bursaries.id=student_bursary_statuses.student_bursary_id
                                        LEFT JOIN student_medical_aids ON students.`id`=student_medical_aids.`student_id`
                                        INNER JOIN bursary_administrator_student ON students.`id`=bursary_administrator_student.`student_id`
                                        WHERE student_bursary_statuses.`status` IN (:status) AND 
                                            bursary_administrator_student.`bursary_administrator_id`=:bursary_administrator_id
                                        GROUP BY monthly_contribution',
        [
            'status' => '1,4,6,7',
            'bursary_administrator_id' => \Auth::user()->userable_id
        ]);

Laravel 日志文件:

[2021-07-16 10:44:34] local.DEBUG: array (
  0 => 
  array (
    'query' => 'SELECT monthly_contribution, COUNT(*) AS contributors FROM students
                                            INNER JOIN student_bursaries ON students.id=student_bursaries.student_id
                                            INNER JOIN student_bursary_statuses ON student_bursaries.id=student_bursary_statuses.student_bursary_id
                                            LEFT JOIN student_medical_aids ON students.`id`=student_medical_aids.`student_id`
                                            INNER JOIN bursary_administrator_student ON students.`id`=bursary_administrator_student.`student_id`
                                            WHERE student_bursary_statuses.`status` IN (:status) AND 
                                                bursary_administrator_student.`bursary_administrator_id`=:bursary_administrator_id
                                            GROUP BY monthly_contribution',
    'bindings' => 
    array (
      'status' => '1,4,6,7',
      'bursary_administrator_id' => 1,
    ),
    'time' => 1.83,
  ),

如果您需要更多数据,请告知?

我在您的查询中看到的是您在 IN() 函数中使用了绑定参数。 如果你想使用它,你必须绑定它们中的每一个。 例如:

DB::select("SELECT * FROM table WHERE id IN (?,?,?,?)", [1,2,3,4]);
DB::select("SELECT * FROM table WHERE id IN (:status1,:status2,:status3,:status4)", [
  'status1' => 1,
  'status2' => 2,
  'status3' => 3,
  'status4' => 4,
]);

对于您的情况,您可以在查询中使用 join 语句。

DB::select("SELECT * FROM table WHERE id IN (". DB::connection()->getPdo()->quote(implode(',', [1,2,3,4])) . ")");

或者使用 Larave 的 ORM 来构建查询。

或者实现其他一些我不推荐的复杂逻辑。