0

如何在 laravel 中进行此查询

SELECT count(*)
  FROM (SELECT *
          FROM REPORT_INBOUND
         WHERE ul_success_inbound = 0
           AND moc_voice_records = 0
           AND regdate = '2019-06-13')
 WHERE gprs_records = 0
   AND moc_sms_records = 0;

我在 laravel 中写了这个查询

$moc = DB::table('REPORT_INBOUND')
        ->select('REPORT_INBOUND.*',
            DB::raw("select *
                       from REPORT_INBOUND
                      where UL_SUCCESS_INBOUND = 0
                        and MOC_VOICE_RECORDS = 0"))
        ->where([['gprs_records', '=', 0], ['moc_sms_records', '=', 0]]);

但它退回了它。我认为这是错误的结果

{"connection":{},"grammar":{},"processor":{},"bindings":{"select":[],"join":[],"where":[0,0],"having":[],"order":[],"union":[]},"aggregate":null,"columns":["REPORT_INBOUND.*",{}],"distinct":false,"from":"REPORT_INBOUND","joins":null,"wheres":[{"type":"Nested","query":{"connection":{},"grammar":{},"processor":{},"bindings":{"select":[],"join":[],"where":[0,0],"having":[],"order":[],"union":[]},"aggregate":null,"columns":null,"distinct":false,"from":"REPORT_INBOUND","joins":null,"wheres":[{"type":"Basic","column":"gprs_records","operator":"=","value":0,"boolean":"and"},{"type":"Basic","column":"moc_sms_records","operator":"=","value":0,"boolean":"and"}],"groups":null,"havings":null,"orders":null,"limit":null,"offset":null,"unions":null,"unionLimit":null,"unionOffset":null,"unionOrders":null,"lock":null,"operators":["=","<",">","<=",">=","<>","!=","<=>","like","like binary","not like","ilike","&","|","^","<<",">>","rlike","regexp","not regexp","~","~*","!~","!~*","similar to","not similar to","not ilike","~~*","!~~*"],"useWritePdo":false},"boolean":"and"}],"groups":null,"havings":null,"orders":null,"limit":null,"offset":null,"unions":null,"unionLimit":null,"unionOffset":null,"unionOrders":null,"lock":null,"operators":["=","<",">","<=",">=","<>","!=","<=>","like","like binary","not like","ilike","&","|","^","<<",">>","rlike","regexp","not regexp","~","~*","!~","!~*","similar to","not similar to","not ilike","~~*","!~~*"],"useWritePdo":false}
4

2 回答 2

1

我什至认为您在这里不需要子查询,以下应该可以工作:

$count = DB::table('REPORT_INBOUND')
    ->where('UL_SUCCESS_INBOUND', '=', 0)
    ->where('MOC_VOICE_RECORDS', '=', 0)
    ->where('regdate', '=', '2019-06-13')
    ->where('gprs_records', '=', 0)
    ->where('moc_sms_records', '=', 0)
    ->count();

也就是说,我建议您只运行以下原始 MySQL 查询:

SELECT COUNT(*)
FROM REPORT_INBOUND
WHERE
    ul_success_inbound = 0 AND
    moc_voice_records = 0 AND
    regdate = '2019-06-13' AND
    gprs_records = 0 AND
    moc_sms_records = 0;
于 2019-06-14T01:25:28.730 回答
0

你的查询是正确的,你只需要打电话->get()

$moc = DB::table('REPORT_INBOUND')
        ->select('REPORT_INBOUND.*',
            DB::raw("select *
                       from REPORT_INBOUND
                      where UL_SUCCESS_INBOUND = 0
                        and MOC_VOICE_RECORDS = 0"))
        ->where([['gprs_records', '=', 0], ['moc_sms_records', '=', 0]])->get()
于 2019-06-14T04:34:47.580 回答