如果在循环中添加了几个"where"和"update",如何生成对db的查询? [英] How to generate a query to db if several “where” and “update” are added in the loop?

查看:64
本文介绍了如果在循环中添加了几个"where"和"update",如何生成对db的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含四个传入数组的方法.

I have a method with four incoming arrays.

前两个数组$whereColumns[]$updateColumns[]包含列名称.

The first two arrays $whereColumns[] and $updateColumns[] contain columns names.

第三列$whereFilters[]包含where方法的过滤器.

The third column $whereFilters[] contains filters for where methods.

最后一个$updateFilters[]包含用于更新的值.

And the last one $updateFilters[] contains values for updating.

如何使用几个where方法和几个更新的列仅生成一个搜索查询?现在,我只知道如何在循环中使用多个where方法创建查询.

How can I generate only one search query with several where methods and several updated columns? Now I know only how create a query with several where methods in a loop.

for($i=0; $<count($whereColumns); $i++){
    $query->where($whereColumns[$i], '=', $whereFilters[$i]);
}
$result = $query->get();

但是如何使用update这样生成查询(但这不起作用):

But how to generate a query with update like this (but this doesn't work):

for($i=0; $i<count($updateColumns); $i++){
    $query->update($updateColumns[$i] => $updateFilters[$i]); 
} 
$result = $query->get();

这两个循环应总共生成一个搜索查询以更新表数据.

Both of these loops should generate one search query in total for updating a table data.

推荐答案

您可以遍历$updateColumns并创建一个包含所有列和值的数组,如下所示: $updateArray = [$column[0] => $value[0], $column[1] => $value[1]];

You can loop through your $updateColumns and create an array containing all columns and values like this: $updateArray = [$column[0] => $value[0], $column[1] => $value[1]];

然后您可以将整个数组放入查询中

Then you can put this whole array into the query

$query->update($updateArray);

这篇关于如果在循环中添加了几个"where"和"update",如何生成对db的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆