连表查询 出现模凌报错
2019-11-07 本文已影响0人
小蝎子tt
在THINKPHP5使用以下链式查询时,pdo报错。生成的sql语句为SELECT * FROM `user` `a` INNER JOIN `userinfo` `i` ON `a`.`email`=`i`.`email` WHERE `email` = :where_email LIMIT 1
$collect=Db::name('article_collect t')
->join('article a','t.article_id=a.article_id')
->where('user_id',$user_id)
->select();
系统显示
SQLSTATE[23000]: Integrity constraint violation: 1052 Column ’email’ in where clause is ambiguous
ambiguous是模棱两可的意思,仔细检查我们发现,
->where('email',$email)
这个语句中的email,我们并不知道是user表的还是userinfo表的,所以报错,解决办法是在设置别名之后,where中使用别名。下面是更正的语句
$collect=Db::name('article_collect t')
->join('article a','t.article_id=a.article_id')
->where('t.user_id',$user_id)
->select();