如何在laravel中做到这一点,子选择

时间:2019-06-14 01:21:52

标签: php laravel

如何在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}

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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()