thinkphp icon indicating copy to clipboard operation
thinkphp copied to clipboard

多对多关联在关联查询时报Column 'id' in where clause is ambiguous错误

Open BunnyShing opened this issue 6 years ago • 0 comments

假设:product表与order表通过product_relate表形成多对多关系 表结构: product:id,product_name order:id,order_number product_relate:id,order_id,product_id

这里因为product_relate也有id字段,tp5.1在组装查询语句时,强制使用了join, 并且还没给主表字段指定表名,导致组装出来的语句在MySQL中会报:Column 'id' in where clause is ambiguous错误

$product = new Product();
$product = $product->where('id',1)->select;
$product->order

我在使用多对多查询的时候发现了这个问题,我看了框架最终组装的sql语句,框架正确的拿到了主表名称,但是就是没有组装主表表名进去,这个设计请问是因为基于其他原因的考虑?如果不是,请问能加上主表表名吗?不然一但要查询多对多关系表时,查询条件就必须加上表名了,如:$product->where('product.id',1)

BunnyShing avatar Jan 02 '20 09:01 BunnyShing