Php Lumen whereRaw with Prepared语句不返回任何内容
我正在尝试使用此查询从数据库中获取值Php Lumen whereRaw with Prepared语句不返回任何内容,php,mysql,lumen,Php,Mysql,Lumen,我正在尝试使用此查询从数据库中获取值 $vouchers = MerchantVoucher::where([ 'merchant_id' => $this->merchant->id, 'code' => $voucherCode ]) ->whereRaw(" ( (valid_from between '{$voucherVa
$vouchers = MerchantVoucher::where([
'merchant_id' => $this->merchant->id,
'code' => $voucherCode
])
->whereRaw("
(
(valid_from between '{$voucherValidFrom}' and '{$voucherValidTo}' ) ||
(valid_to between '{$voucherValidFrom}' and '{$voucherValidTo}')
)"
)
->count();
通过这个查询,我得到了预期的结果,它返回的行数为1。
我尝试使用下面这个查询的Prepared语句优化查询
$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
使用的框架:内腔
数据库:MySQL您需要删除占位符周围的引号:
是否介于两者之间?而且?
现在无法验证我的假设,但我认为您需要删除标识符周围的引号:有效吗?还有?
@Fitzi你的假设解决了我的问题:),谢谢!那我就回答你