Laravel SQL 结果与 MySQL 查询不同

问题描述

从我的各种搜索中,我怀疑他下面的问题源于我使用 GROUP BY 的事实。然而,我遇到的所有例子都没有我的那么简单,他们的解决方案最终让我无法理解。我希望你能帮我弄清楚这里出了什么问题?

我在我的应用程序中使用了多个连接的查询。当我在远程连接到宅基地 VM 的 MysqL 客户端 (sqlYog) 中运行查询时 - 我得到了正确的结果。当我通过 SSH 连接到 VM 并运行 MysqL 并运行查询时,我得到了正确的结果。 然而,当查询在 Laravel/应用程序中运行时,结果是不同的,我不明白为什么!

查询

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) 但它返回 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,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,'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(',',4])) . ")");

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

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