tp5 关联查询 报错:Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous

tp5 做关联查询,想要添加一个where条件,对id进行筛选,代码如下

$customWhere = ['id' => ['in',$websiteId]];
$list = $this->model
                ->with(['admin','category','language'])
                ->where($where)
                ->where($customWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

运行时报错:Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous

原因:几个表都有id字段,不确定是差的哪个表对应的id

解决:在customWhere条件中,id前面加上表全名(数据库中表对应的名字是什么就写什么),修改后的代码如下

$customWhere = ['External_links.id' => ['in',$websiteId]];
$list = $this->model
                ->with(['admin','category','language'])
                ->where($where)
                ->where($customWhere)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

 

posted @ 2022-05-18 10:28  大雄呀  阅读(2695)  评论(0编辑  收藏  举报