我是mysql查询的新手,我有一个查询将搜索数组内的所有项目
status in ('php','laravel','apiato')
如果我像这样使用它就可以工作,现在我必须向数组添加更多值而不是传递我制作的字符串该文件中的常量文件我想发送一个数组,它抛出异常Array To string Conversion
public function getALlData(){
$array = BooksConstants::Status_constants;
DB::select("SELECT count(1) as total_transactions from books where books.account_id in ('$this->account_ids') and DATE(created_at) between ? and ? and status in $array ", [$fromDate, $toDate]);
}
首先我们使用
implode()
将数组转换为字符串,然后在 array_map()
函数中使用 trim()
函数删除空格。 array_map()
函数将数组的每个值发送到用户编写的函数。
public function getALlData()
{
$array = BooksConstants::Status_constants;
DB::select(
"SELECT count(1) as total_transactions
from books
where books.account_id in ('$this->account_ids')
and DATE(created_at) between ? and ?
and status in ('" . implode("','", array_map('trim', $array)) ."') ",
[$fromDate, $toDate]
);
}