laravel:查询生成器返回的结果与实际的mysql查询不同

cwdobuhd  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(342)

我有一个列名为firmware\u flash\u result的表,它不是fail就是pass。查询生成器为pass返回正确的结果,但为failure返回错误的结果。
实际查询及结果:

SELECT COUNT(DISTINCT `payment_module_id`) 
 FROM `payment_prod_pograms` 
 WHERE `created_at` >= "2018-09-17 00:00:00" AND 
       (
        `payment_module_id` LIKE "PB10____18____" OR 
        `payment_module_id` LIKE "PM10____18____" 
       ) AND 
       CHAR_LENGTH(`payment_module_id`) = 14 AND 
       `firmware_flash_result` = "FAIL";

+-------------------------------------+
| COUNT(DISTINCT `payment_module_id`) |
+-------------------------------------+
|                                   0 |
+-------------------------------------+

laravel查询生成器:

$payment_prod_failed_test = DB::table('payment_prod_pograms')
        ->where('created_at','>=','2018-09-17 00:00:00')
        ->whereRaw('char_length(payment_module_id) = 14')
        ->where('payment_module_id','like','%PB10____18____%')
        ->orWhere('payment_module_id','like','%PM10____18____%')
        ->where('firmware_flash_result','=','FAIL')
        ->distinct()
        ->count('payment_module_id');

上面的查询返回的结果是453,这是不可能的。
经过一番调试,我发现:

->orWhere('payment_module_id','like','%PM10____18____%')

导致这个错误的原因是什么?
数据库中有1515个条目,所有条目都通过了,但此查询返回的结果不同。。我做错什么了?任何帮助都将不胜感激

yi0zb3m4

yi0zb3m41#

您正在将您的where payment\u模块\u id分组为%pb10\uuuuuuuuuuuuuuuuuuuuuuuuuuuuu%或%pm10\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。这是通过使用闭包来完成的。

$payment_prod_failed_test = DB::table('payment_prod_pograms')
        ->where('created_at','>=','2018-09-17 00:00:00')
        ->whereRaw('char_length(payment_module_id) = 14')
        ->where(function($query) { 
            $query->where('payment_module_id','like','%PB10____18____%')
                  ->orWhere('payment_module_id','like','%PM10____18____%');
        })
        ->where('firmware_flash_result','=','FAIL')
        ->distinct()
        ->count('payment_module_id');

相关问题