Php 这些Laravel查询绑定不起作用
为什么我的Laravel查询绑定不起作用 此查询在MySqlWorkbench中工作:Php 这些Laravel查询绑定不起作用,php,mysql,laravel,laravel-5,laravel-5.4,Php,Mysql,Laravel,Laravel 5,Laravel 5.4,为什么我的Laravel查询绑定不起作用 此查询在MySqlWorkbench中工作: SELECT COUNT(id) AS AC_Leads, SUM(IF(webinarMinsWatched > 0, 1, 0)) AS startedWebinar, SUM(IF(webinarMinsWatched >= 47.23, 1, 0)) AS reachedCta FROM contacts WHERE DATE_FORMAT(cre
SELECT
COUNT(id) AS AC_Leads,
SUM(IF(webinarMinsWatched > 0, 1, 0)) AS startedWebinar,
SUM(IF(webinarMinsWatched >= 47.23, 1, 0)) AS reachedCta
FROM
contacts
WHERE
DATE_FORMAT(created_at, '%Y-%m-%d') = '2017-05-06'
AND adId = '6000689619221'
GROUP BY adId
这是我的拉威尔密码。不幸的是,它返回一个空数组,与上面的原始MySql不同,后者返回正确的结果:
$webinarCta = '47.23';
$adId = "6000689619221";
$dateStr = "2017-05-06";
DB::enableQueryLog();
$statsQuery = DB::table('contacts')
->selectRaw('COUNT(id) as AC_Leads, SUM(IF(webinarMinsWatched > 0, 1, 0)) as startedWebinar, SUM(IF(webinarMinsWatched >= ?, 1, 0)) as reachedCta', [(float) $webinarCta])
->whereRaw("DATE_FORMAT(created_at, '%Y-%m-%d') = '?'", [$dateStr])
->whereRaw("adId = '?'", [(int) $adId])
->groupBy('adId');
print_r($statsQuery->toSql());
echo '<hr>';
$stats = $statsQuery->get();
print_r($stats);
echo '<hr>';
Log::debug(DB::getQueryLog());
dd(DB::getQueryLog());
这是最终对我有效的查询。在?周围没有引号
谢谢,@Razor.我认为绑定不应该被引用,用?替换“?”,并且不要向intThank扔$adId,@Razor;是的,我认为引用可能是问题所在。
array:1 [▼
0 => array:3 [▼
"query" => "select COUNT(id) as AC_Leads, SUM(IF(webinarMinsWatched > 0, 1, 0)) as startedWebinar, SUM(IF(webinarMinsWatched >= ?, 1, 0)) as reachedCta from `contacts` where DATE_FORMAT(created_at, '%Y-%m-%d') = '?' and adId = '?' group by `adId` ◀"
"bindings" => array:3 [▼
0 => 47.23
1 => "2017-05-06"
2 => 6000689619221
]
"time" => 0.38
]
]
$statsQuery = DB::table('contacts')
->selectRaw('adId, COUNT(id) as AC_Leads, SUM(IF(webinarMinsWatched > 0, 1, 0)) as startedWebinar, SUM(IF(webinarMinsWatched >= ?, 1, 0)) as reachedCta', [(float) self::WEBINAR_CTA])
->groupBy('adId');
if ($startDate) {
$statsQuery->whereRaw("CONVERT_TZ(created_at, 'UTC', ?) >= STR_TO_DATE(?, '%Y-%m-%d')", [$reportingClientTimeZone, $startDate]); //https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_convert-tz
}
if ($endDate) {
$statsQuery->whereRaw("CONVERT_TZ(created_at, 'UTC', ?) < DATE_ADD(STR_TO_DATE(?, '%Y-%m-%d'), INTERVAL 1 day)", [$reportingClientTimeZone, $endDate]); //Given a certain endDate (which begins at midnight), only include results LESS THAN 1 day after that midnight
}
$result = $statsQuery->get();