我想问问我如何在查询中输入参数?
这是我在Laravel中编写的代码。
public function trans () {
$query = DB::table('transaction')
->join('users', 'users.id', '=', 'transaction.user_id')
->select('transaction.user_id', 'users.name', 'users.email', 'users.gender', 'users.birthday')
->selectRaw('EXTRACT (YEAR FROM AGE (birthday)) as age, COUNT(transaction.id) as trans_total, AVG(transaction.amount) as average')
->where('transaction.type', '=', 'cr')
->whereIn('transaction.description', function ($subQuery) {
$subQuery->selectRaw('description')->from('transaction')->where('description', 'SIMILAR TO', '%(Buy)%');
})->whereBetween('transaction.created_at', [$start->format('Y-m-d'), $end->format('Y-m-d')])
->groupBy([
'users.id',
'name',
'gender',
'transaction.user_id',
]);
if ($this->request-> == 'min') {
$query->whereIn('transaction.amount', function ($subQuery) use($av) {
$subQuery->selectRaw('AVG(transaction.amount) as average FROM transaction where average <= :av', [
'av' => $av
]);
});
}
$paginate = $query->paginate();
return $paginate;
}
在sql查询中。
select "transaction"."user_id",
"users"."name",
"users"."email",
"users"."gender",
"users"."birthday", EXTRACT (YEAR FROM AGE (birthday)) as age, COUNT(transaction.id) as trans_total, AVG(transaction.amount) as average from "transaction"
inner join "users" on "users"."id" = "transaction"."user_id" where "transaction"."type" = ? and "transaction"."description"
in (select description from "transaction" where "description" SIMILAR TO ?) and "transaction"."created_at" between ? and ? and "transaction"."amount"
in (select AVG(transaction.amount) as average FROM transaction where average <= :av) group by "users"."id",
"name",
"gender",
"transaction"."user_id"
但我收到了此消息
无效的参数编号:混合的命名参数和位置参数
我要过滤的计划的平均金额小于指定参数$ av的金额。
谢谢。