Codeigniter 的活动记录中的 mysql 查询

发布于 2024-12-11 12:19:17 字数 552 浏览 0 评论 0原文

我正在运行这个查询,

$this->db->select('news.news_id, news.title, news.article, news.date_posted, news_assets.news_assets_id, news_assets.url')
        ->from('news_assets')
        ->join('news', 'news_assets.news_news_id = news.news_id', 'left')
        ->order_by('news.date_posted', 'DESC');

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

        return $query->row_array();

现在这个查询返回一篇新闻文章,它应该返回新闻资产表中的任何属性资产,新闻资产表与新闻有 1:n 关系,所以新闻文章可能有无限数量的资产,但是一项资产只有 1 篇新闻文章。

我的问题是,当我运行此查询时,仅返回新闻文章的一项资产,这是为什么?

I am running this query,

$this->db->select('news.news_id, news.title, news.article, news.date_posted, news_assets.news_assets_id, news_assets.url')
        ->from('news_assets')
        ->join('news', 'news_assets.news_news_id = news.news_id', 'left')
        ->order_by('news.date_posted', 'DESC');

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

        return $query->row_array();

Now this query returns a news article, and it should return any attributed assets from the news assets table, the news assets table has 1:n relationship with news so a news article may have an infinite amount of assets but an asset with only ever have 1 news article.

My question is that when I run this query only one asset for a news article is returned, why would this be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云裳 2024-12-18 12:19:17

首先,就我的想法而言,您应该选择新闻并加入 news_assets 因为据我了解,您的资产属于一篇新闻文章。

另外,您还有 $query->row_array(); ,它仅返回一行,但如果您有 1+ 行,则应该使用,根据 这个

foreach ($query->result_array() as $row) {
   echo $row['title'];
   echo $row['name'];
}

First of all, as for my thoughts, you should select news and join news_assets because your assets belongs to one news article as I understand.

Also you have $query->row_array(); which returns just one row, but if you have 1+ rows you should use, according to this:

foreach ($query->result_array() as $row) {
   echo $row['title'];
   echo $row['name'];
}
血之狂魔 2024-12-18 12:19:17

也许你的资产表中确实有一行,但如果没有,我猜你的 JOIN 是错误的,
也许你正在向左而不是向右或以其他方式做

maybe you really have one row in the assets table but if not, i guess your JOIN is wrong,
maybe you are doing left instead right or the other way

你对谁都笑 2024-12-18 12:19:17

尝试下面的查询。

    $this->db->select('news.news_id, news.title, news.article, news.date_posted, news_assets.news_assets_id, news_assets.url');
    $this->db->from('news_assets');
    $this->db->join('news', 'news.news_id=news_assets.news_news_id', 'left');
    $this->db->order_by('news.date_posted', 'DESC');

Try below Query.

    $this->db->select('news.news_id, news.title, news.article, news.date_posted, news_assets.news_assets_id, news_assets.url');
    $this->db->from('news_assets');
    $this->db->join('news', 'news.news_id=news_assets.news_news_id', 'left');
    $this->db->order_by('news.date_posted', 'DESC');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文