gpt4 book ai didi

php - Lumen whereRaw 与准备好的语句不返回任何内容

转载 作者:行者123 更新时间:2023-11-29 09:28:40 25 4
gpt4 key购买 nike

我正在尝试使用此查询从数据库中获取值

$vouchers = MerchantVoucher::where([
'merchant_id' => $this->merchant->id,
'code' => $voucherCode
])
->whereRaw("
(
(valid_from between '{$voucherValidFrom}' and '{$voucherValidTo}' ) ||
(valid_to between '{$voucherValidFrom}' and '{$voucherValidTo}')
)"
)
->count();

通过这个查询,我得到了预期的结果,它返回一个行数。我尝试使用下面的查询的准备语句来优化查询

    $vouchers = MerchantVoucher::where([
'merchant_id' => $this->merchant->id,
'code' => $voucherCode
])
->whereRaw("
(
(valid_from between '?' and '?' ) ||
(valid_to between '?' and '?' )
)",
[
$voucherValidFrom,
$voucherValidTo,
$voucherValidFrom,
$voucherValidTo
]
)
->count();

当我使用此查询时,它返回 0。

有人对此有解释吗?预先感谢您!

非 PHP 用户的原始 Sql 比较

 select count(*) from `merchant_vouchers` where (`merchant_id` = ? and `code` = ?) and 
(
(valid_from between '2019-12-03 11:36:35' and '2019-12-10 11:36:35' ) ||
(valid_to between '2019-12-03 11:36:35' and '2019-12-10 11:36:35')
) and `merchant_vouchers`.`deleted_at` is null

返回1

  select count(*) from `merchant_vouchers` where (`merchant_id` = ? and `code` = ?) and 
(
(valid_from between '?' and '?' ) ||
(valid_to between '?' and '?' )
) and
`merchant_vouchers`.`deleted_at` is null

返回0

使用的框架:Lumen

数据库:MySQL

最佳答案

您需要删除占位符周围的引号:valid_from 之间?和?

关于php - Lumen whereRaw 与准备好的语句不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59155944/

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