gpt4 book ai didi

php - 在 Laravel 中将数据绑定(bind)到 DB::raw()

转载 作者:行者123 更新时间:2023-11-29 04:42:07 25 4
gpt4 key购买 nike

我用原始 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 在后台设置的绑定(bind)。我确实尝试用这个再次填写它们:

$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();

虽然它不会抛出错误,但它不会返回任何数据。

所以,我的问题是:如何在这样的查询中使用带有绑定(bind)参数的 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

最佳答案

不要使用 setBindings,因为您不需要覆盖所有 where 绑定(bind)(您就是这样做的),只需这样做:

$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(*)'))
->addBinding($start_date, 'select')
->where('actual_date', '>', $start_date)
...

关于php - 在 Laravel 中将数据绑定(bind)到 DB::raw(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26245754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com