Home  >  Q&A  >  body text

yii2 mysql如何跨数据库联表查询?

wp_post在blog_cn数据库中,而stat_weibo在task数据库中。现在两个数据表需要进行连表查询,该如何实现?

大家讲道理大家讲道理2743 days ago818

reply all(3)I'll reply

  • 阿神

    阿神2017-04-17 15:43:34

    Also note that these two databases are in one mariadb instance. Cross-mariadb, cross-server join is not possible

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 15:43:34

    Override the ActiveRecord::getDb() method and return to connect to the database according to the configured db component by default.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 15:43:34

    You can do this if the two databases are in the same Mysql server

    Override the tableName() method of the Weibo model class:

    public static function tableName()
    {
        preg_match("/dbname=([^;]+)/i", self::getDb()->dsn, $matches);
        return $matches[1].'.weibo';
    }

    If the two databases are not in the same Mysql server, there is no solution for the time being

    reply
    0
  • Cancelreply