联表查询时,相同字段未取别名导致报错(mysql)

Integrity constraint violation – yii\db\IntegrityException

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'company_id' in where clause is ambiguous The SQL being executed was: SELECT COUNT(*) FROM `crm_customer` LEFT JOIN `crm_customer_combine` `cc` ON `crm_customer`.`id` = `cc`.`customer_id` WHERE (`company_id`=11) AND ((`cc`.`level`=1) AND (`cc`.`administrator_id`=1))

Error Info: Array
(
    [0] => 23000
    [1] => 1052
    [2] => Column 'company_id' in where clause is ambiguous
)

Caused by: PDOException

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'company_id' in where clause is ambiguous

出现相同的字段报错:SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'company_id' in where clause is ambiguous

解决:取别名

$query = MysqlTableName::find()->alias('c');

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券