我的 php/laravel 代码中有一个 select wuery 问题是当我运行它时出现此错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column '4' in 'where clause'
where (`element_value`.`p_id` = `4`))
Run Code Online (Sandbox Code Playgroud)
这是我的查询
DB::table('value_tbl')
->select(DB::raw("CONCAT(values.value_name,'/',element_value.qan) AS full_value"))
->leftJoin('element_value','element_value.element_id','value.element_id')
->where (['element_value.p_id'=>$a->p_id])->get()->pluck('full_value')->toArray())
Run Code Online (Sandbox Code Playgroud)
小智 5
如果要使用运算符,where 函数接受三个参数:第一个是列名,第二个是运算符,第三个是要比较的值。
将您的条件更改为如下所示
->where('element_value.p_id', '=', $a->p_id)
// or, because you are making an exact match, do this
->where('element_value.p_id', $a->p_id)
Run Code Online (Sandbox Code Playgroud)
您目前拥有的代码变得混乱,因为您告诉 Laravel 您正在通过使用数组作为 where 第一个参数来创建多个 where 条件。
然后 Laravel 获取该数组的值并尝试将其转换为列、运算符和值 - 就像上面的代码片段那样。
如果您真的想使用数组,则需要执行以下操作:
->where([
['element_value.p_id', '=', $a->p_id]
])
// or, because you are making an exact match, do this
->where([
['element_value.p_id', $a->p_id]
])
Run Code Online (Sandbox Code Playgroud)
请注意我们如何传递两组数组?
这是因为 Laravel 要么希望将每个参数分开,要么希望包含正确签名的数组数组。
如需更详细的答案;签名看起来像这样
public function where($column, $operator = null, $value = null, $boolean = 'and')
Run Code Online (Sandbox Code Playgroud)
如果$column是数组,则假定将数组数组传递到该方法中,并且每个子数组将有效地分布在上述签名中。
当您将单个数组传递给该方法时,Laravel 正在获取 的值,4然后将其用作$column.
这是因为您为该数组使用了键值,因此您使用的列名实际上被忽略了。
关于这个方法的另一个有趣的事情是它有以下片段:
// If the given operator is not found in the list of valid operators we will
// assume that the developer is just short-cutting the '=' operators and
// we will set the operators to '=' and set the values appropriately.
if ($this->invalidOperator($operator)) {
[$value, $operator] = [$operator, '='];
}
Run Code Online (Sandbox Code Playgroud)
这允许我们不传入操作符,而 Laravel 只会假设我们想要使用 an=来代替。
这就是我能够从上面的例子中省略它的方式。
| 归档时间: |
|
| 查看次数: |
82 次 |
| 最近记录: |