【问题标题】:Error when joining multiple times the same table多次加入同一个表时出错
【发布时间】:2013-11-24 13:45:58
【问题描述】:

在一张桌子上工作不止一次时,是否可以连接多个桌子?让我解释一下。

我有三个表categoriescontentmembers
顺便说一句,使用 CodeIgniter 的 ActiveRecord。这是我的content_model.php

public function get_content_list(){

    $this->db
        ->select('
            content.id,
            categories.id as catid,
            categories.title as category,
            content.alvl,
            content.ordering,
            content.state,
            content.title,
            content.slug,
            content.text,
            content.views,
            content.aid,
            members.username as author,
        ')
        ->join('categories', 'content.catid = categories.id')
        ->join('categories', 'content.alvl = categories.id') // MARK01
        ->join('members', 'content.aid = members.id');

    $query = $this->db->get($this->_table_name);

    if($query->num_rows() > 0)
        return $query->result();
    else
        return FALSE;

}

我留下第二行注释为 MARK01,这就是问题的开始。

现在澄清一下,类别存储在一个 DB 表 categories 中,用于 memberscontents(页面、文章等)、banners 等等。 . 有一列categories.cid 代表组件的ID,并连接到DB 表components。因此,每当我需要检索成员的类别时,我将使用WHEREcategories.cid = 3。它工作正常,直到我需要从多个表中检索 JOINed 记录,同时多次询问同一个表。

这是一个var_dump(),带有注释行以进行解释,并注释了 MARK01 行以防止出现错误:

array (size=3)
  0 => 
    object(stdClass)[41]
      public 'id' => string '1' (length=1)
      public 'catid' => string '5' (length=1)
      public 'category' => string 'News' (length=4)
      public 'alvl' => string '10' (length=2) // Access level
      public 'ordering' => string '1' (length=1)
      public 'state' => string '1' (length=1) // Status ON/OFF
      public 'title' => string 'Qualification for a new production set' (length=38)
      public 'slug' => string 'qualification-for-a-new-production-set' (length=38)
      public 'text' => string 'Very long text blablabla"'... (length=2629)
      public 'views' => string '13' (length=3)
      public 'aid' => string '1' (length=1)  // Author ID
      public 'author' => string 'aspirinemaga' (length=12)

这就是我取消注释第二行时得到的结果MARK01

A Database Error Occurred
Error Number: 1066
Not unique table/alias: 'cis_categories'
SELECT `cis_content`.`id`, `cis_categories`.`id` as catid, `cis_categories`.`title` as category, `cis_content`.`alvl`, `cis_content`.`ordering`, `cis_content`.`state`, `cis_content`.`title`, `cis_content`.`slug`, `cis_content`.`text`, `cis_content`.`views`, `cis_content`.`aid`, `cis_members`.`username` as author FROM (`cis_content`) JOIN `cis_categories` ON `cis_content`.`catid` = `cis_categories`.`id` JOIN `cis_categories` ON `cis_content`.`alvl` = `cis_categories`.`id` JOIN `cis_members` ON `cis_content`.`aid` = `cis_members`.`id`
Filename: D:\wamp\www\cmstut\system\database\DB_driver.php
Line Number: 330

知道这里有什么问题吗?

【问题讨论】:

    标签: php mysql database codeigniter activerecord


    【解决方案1】:

    如果你 JOIN 表超过 1 次,你必须给他们不同的名字。

    ... JOIN categories categories2 ON content.catid = categories2.id...
    

    在这种情况下,您应该使用“categories2”来定义“ON”。对于“SELECT”,也使用您的新名称

    对于codeigniter活动记录你可以试试

    $this->db->join('categories AS categories2',...)
    

    【讨论】:

    • 对于codeigniter活动记录你可以试试$this->db->join('categories AS categories2',...)
    • 正是我想要的。谢谢你 Mykyta!
    • 如果你也能用你的评论更新你的答案会很好。
    猜你喜欢
    • 2023-03-20
    • 2021-04-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多