【发布时间】:2014-12-02 10:55:53
【问题描述】:
我用原始 SQL 编写了一个查询,它提取给定日期范围内的所有记录,根据插入的日期和小时计算记录,并计算自插入“开始日期”以来的小时数。
我现在正在尝试将该查询填充到 Laravel 中。我遇到的问题是我需要一些在 Eloquent 文档中找不到的东西,所以我不得不使用 DB::raw() 来完成它们。这适用于硬编码值,但后来我到了需要使用动态值的地步,这让我找到了this question。这个问题几乎奏效了,但我遇到的问题是,由于我使用带变量的 where 子句,它会导致一些奇怪的副作用。
例如,使用这个查询:
$clicks = Tracker::select(DB::raw("TIME_TO_SEC(TIMEDIFF(DATE_FORMAT(actual_date,'%Y-%m-%d %H:00:00'),'?'))/60/60+1 as hours_since_send"), DB::raw('COUNT(*)'))
->where('actual_date', '>', $start_date)
->where('actual_date', '<', $end_date)
->whereIn('link_id', $link_ids)
->groupBy(DB::raw('DAY(actual_date)'))
->groupBy(DB::raw('HOUR(actual_date)'))
->orderBy('actual_date', 'asc')
->setBindings([$start_date])
->get();
给我这个错误:
SQLSTATE[HY093]: Invalid parameter number (SQL: select TIME_TO_SEC(TIMEDIFF(DATE_FORMAT(actual_date,'%Y-%m-%d %H:00:00'),'2014-10-02 00:00:00'))/60/60+1 as hours_since_send, COUNT(*) from `trackers` where `actual_date` > ? and `actual_date` < ? and `link_id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) group by DAY(actual_date), HOUR(actual_date) order by `actual_date` asc)
这似乎很简单,所以我尝试了:
$clicks = Tracker::select(DB::raw("TIME_TO_SEC(TIMEDIFF(DATE_FORMAT(actual_date,'%Y-%m-%d %H:00:00'),'?'))/60/60+1 as hours_since_send"), DB::raw('COUNT(*)'))
->where('actual_date', '>', $start_date)
->where('actual_date', '<', $end_date)
->whereIn('link_id', $link_ids)
->groupBy(DB::raw('DAY(actual_date)'))
->groupBy(DB::raw('HOUR(actual_date)'))
->orderBy('actual_date', 'asc')
->setBindings(array_merge([$start_date], $link_ids))
->get();
这给了我错误:
SQLSTATE[HY093]: Invalid parameter number (SQL: select TIME_TO_SEC(TIMEDIFF(DATE_FORMAT(actual_date,'%Y-%m-%d %H:00:00'),'2014-10-02 00:00:00'))/60/60+1 as hours_since_send, COUNT(*) from `trackers` where `actual_date` > 1156 and `actual_date` < 1157 and `link_id` in (1158, 1159, 1160, 1161, 1162, 1163, 1164, 1165, 1166, 1167, 1168, 1169, 1170, 1171, ?, ?) group by DAY(actual_date), HOUR(actual_date) order by `actual_date` asc)
看来setBindings 只是覆盖了 laravel 在后台设置的绑定。我确实尝试用这个再次填写它们:
$clicks = Tracker::select(DB::raw("TIME_TO_SEC(TIMEDIFF(DATE_FORMAT(actual_date,'%Y-%m-%d %H:00:00'),'?'))/60/60+1 as hours_since_send"), DB::raw('COUNT(*)'))
->where('actual_date', '>', $start_date)
->where('actual_date', '<', $end_date)
->whereIn('link_id', $link_ids)
->groupBy(DB::raw('DAY(actual_date)'))
->groupBy(DB::raw('HOUR(actual_date)'))
->orderBy('actual_date', 'asc')
->setBindings(array_merge([$start_date, $start_date, $end_date], $link_ids))
->get();
虽然 is 不会抛出错误,但它不会返回任何数据。
所以,我的问题是:如何在这样的查询中使用我的 DB::raw 表达式和绑定参数,而不会弄乱后台的 laravel 集?或者我怎样才能重写这个查询来完成我需要的?
这是原始查询供参考:
SELECT
TIME_TO_SEC(TIMEDIFF(DATE_FORMAT(actual_date,'%Y-%m-%d %H:00:00'),'2014-10-02 00:00:00'))/60/60+1 as hours_since_send,
COUNT(*)
FROM
trackers
WHERE
link_id BETWEEN 1156 AND 1171
AND
actual_date > '2014-10-02 00:00:00'
AND actual_date < '2014-10-08 00:00:00'
GROUP BY
DAY(actual_date),
HOUR(actual_date)
ORDER BY
actual_date
【问题讨论】:
标签: php mysql laravel-4 eloquent