如何将外部数组传​​递给查询?

发布于 2025-01-16 13:34:10 字数 478 浏览 0 评论 0原文

我是 mysql 查询的新手,我有一个查询将搜索数组 status in ('php','laravel','apiato') 中的所有项目,如果我这样使用它就可以工作,现在我必须向数组添加更多值,而不是传递字符串,我从该文件创建了一个常量文件,我想发送一个数组,它抛出一个异常数组到字符串转换

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]);
}

I am new to the mysql queries, i have one query which will search all the items inside the array status in ('php','laravel','apiato') if i use like this it's working, now i have to add more values to the array instead of passing strings i made one constant file from that file i want to send an array,it's throwing an exception 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]);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫林﹌晚霞¤ 2025-01-23 13:34:10

首先,我们使用 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]
    );
}

First we use implode() to convert array to string then inside array_map() function use trim() function to remove whitespaces. The array_map() function sends each value of an array to a user-made function.

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]
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文