diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index ebab051add290d62c01f2be4b2abb8bdf6c2a362..240d2ca4060a285d14d71a4bfdc185e14ebf385e 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -164,7 +164,7 @@ class HasMany extends Relation } } $localKey = $this->localKey ?: $this->parent->getPk(); - return $this->query->whereExp($this->foreignKey, '=' . $this->parent->getTable() . '.' . $localKey)->fetchSql()->count(); + return $this->query->alias( 'count_table')->whereExp( 'count_table.' . $this->foreignKey, '=' . $this->parent->getTable() . '.' . $localKey)->fetchSql()->count(); } /**