我的查询返回错误的结果,我认为这是因为最终参数(分钟)没有绑定到查询。当我得到querylog一切似乎都很好,但错误的结果正在返回。如果我将minutes参数直接放入查询中,它会按预期返回正确的结果,但该值必须是变量。
要解释查询,它会计算所有记录,直到总分钟达到设定数字,在我的示例中使用500。
此查询可以正常工作并返回结果:
DB::select('SELECT NULL AS session_total_charges, NULL AS call_date, NULL AS inbound_duration, NULL AS total
FROM dual
WHERE @total := 0
UNION ALL
SELECT session_total_charges, call_date, inbound_duration, @total := @total + inbound_duration AS total
FROM (SELECT * FROM records ORDER BY call_date) C where calling_user =:user and call_date LIKE :date AND outbound_zone_id IN ("UKX","UKM","UKLR","UKNR") and @total < 500', ["user"=>$user, "date"=>$date]);
此查询不起作用,只返回一行
DB::select('SELECT NULL AS session_total_charges, NULL AS call_date, NULL AS inbound_duration, NULL AS total
FROM dual
WHERE @total := 0
UNION ALL
SELECT session_total_charges, call_date, inbound_duration, @total := @total + inbound_duration AS total
FROM (SELECT * FROM records ORDER BY call_date) C where calling_user =:user and call_date LIKE :date AND outbound_zone_id IN ("UKX","UKM","UKLR","UKNR") and @total < :minutes', ["user"=>$user, "date"=>$date, "minutes"=>$minutes]);
[query] => SELECT NULL AS session_total_charges, NULL AS call_date, NULL AS inbound_duration, NULL AS total
FROM dual
WHERE @total := 0
UNION ALL
SELECT session_total_charges, call_date, inbound_duration, @total := @total + inbound_duration AS total
FROM (SELECT * FROM records ORDER BY call_date) C where calling_user =:user and call_date LIKE :date AND outbound_zone_id IN ("UKX","UKM","UKLR","UKNR") and @total < :minutes
[bindings] => Array
(
[user] => T-M000005251-009
[minutes] => 500
[date] => 2016-12-%%
)
//Result using bindings (only one row returned)
[0] => stdClass Object
(
[session_total_charges] => 0.014125
[call_date] => 2016-12-01 09:12:39
[inbound_duration] => 113
[total] => 113
)
//Result with values inserted directly into query(correct result returned)
[0] => stdClass Object
(
[session_total_charges] => 0.014125
[call_date] => 2016-12-01 09:12:39
[inbound_duration] => 113
[total] => 113
)
[1] => stdClass Object
(
[session_total_charges] => 0.04733333
[call_date] => 2016-12-01 09:18:16
[inbound_duration] => 142
[total] => 255
)
[2] => stdClass Object
(
[session_total_charges] => 0.03866667
[call_date] => 2016-12-01 09:22:21
[inbound_duration] => 116
[total] => 371
)
[3] => stdClass Object
(
[session_total_charges] => 0.012625
[call_date] => 2016-12-01 09:29:24
[inbound_duration] => 101
[total] => 472
)
[4] => stdClass Object
(
[session_total_charges] => 0.0505
[call_date] => 2016-12-01 12:03:16
[inbound_duration] => 404
[total] => 876
)