【问题标题】:How to join two tables in Codeigniter如何在 Codeigniter 中连接两个表
【发布时间】:2010-12-20 14:16:29
【问题描述】:

我尝试加入 2 个表并输出结果:

在模型中:

$this->db->select('*');
$this->db->from('news');
$this->db->join('authors', 'authors.id = news.author_id');

在查看文件中:

foreach($item as $row):
    echo $row->id;
endforeach;

当我运行上面的代码时,它会从authors 表中输出id 列。 我的问题是如何从news 表中回显id 列?

newsauthor 表都有名为 id 的列。

【问题讨论】:

    标签: php sql codeigniter join


    【解决方案1】:

    试试echo $row->author_id

    新闻表中您想要的字段似乎是author_id,而不是id

    编辑否则,您可以在您的选择语句中使用as sql 关键字为您的字段指定不同的名称。

    $this->db->select('*, news.id as my_news_id');
    

    然后,echo $row->my_news_id

    【讨论】:

    • 我想从新闻表中回显 id
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 2017-02-24
    • 2019-08-17
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 2015-11-26
    相关资源
    最近更新 更多