Codeigniter 的活动记录中的 mysql 查询
我正在运行这个查询,
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,就我的想法而言,您应该选择新闻并加入
news_assets
因为据我了解,您的资产属于一篇新闻文章。另外,您还有
$query->row_array();
,它仅返回一行,但如果您有 1+ 行,则应该使用,根据 这个: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:也许你的资产表中确实有一行,但如果没有,我猜你的 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
尝试下面的查询。
Try below Query.