doujiexi1824 2011-10-25 09:48
浏览 53
已采纳

codeigniter活动记录中的mysql查询

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?

  • 写回答

3条回答 默认 最新

  • doulian4467 2011-10-25 10:07
    关注

    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'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line