我在这样的laravel中得到一个SQL
$scheduleMap=array("schedule_id"=>$scheduleId);
$scheduleConsume=DB::connection("mysql_report")->table("xxx")
->where($scheduleMap)
->whereBetween(DB::Raw("TO_DAYS(FROM_UNIXTIME(report_time))"),array(DB::Raw("TO_DAYS('".$startDate."')"),DB::Raw("TO_DAYS('".$startDate."')")))
->select(DB::Raw("SUM(imp_num) as imp_num"))
->first();
我将SQL打印为
select SUM(imp_num) as imp_num from `xxx`
where TO_DAYS(FROM_UNIXTIME(report_time))
between TO_DAYS('2016-11-05')
and TO_DAYS('2016-11-12')
然后我尝试在navicat中运行sql,它运行良好,但在laravel中,它返回null。 我无法理解!我现在必须使用whereRaw。
答案 0 :(得分:0)
试试这个:)
$scheduleConsume = DB::connection("mysql_report")
->table('xxx')
->where('schedule_id', $scheduleId)
->whereBetween(
DB::Raw("TO_DAYS(FROM_UNIXTIME(report_time))"), [
DB::Raw("TO_DAYS('".$startDate."')"),DB::Raw("TO_DAYS('".$startDate."')")
]
)
->select(DB::Raw("SUM(imp_num) as imp_num"))
->first();