简体   繁体   English

两个表连接两个结果

[英]Two tables join two results

I have a two tables one with blog news and another with articles. 我有两张桌子,一张是博客新闻,另一张是文章。 Simply what i need to do is join them so i have code like this 我要做的只是加入他们,所以我有这样的代码

$query = $this->db->select('*')
              ->from('blogs')
          ->select('blogs.id as BlogId....')
      ->join('articles', 'articles.highlight = 1', 'left')
      ->join('authors', 'articles.author = authors.id', 'left')
      ->join('companies', 'authors.company_id = companies.id')
      ->get();

Join is working ok but then i need to do echo for data in foreach and here my problem comes. 联接工作正常,但随后我需要在foreach中对数据进行回显,这就是我的问题了。 I have duplicated blogs under articles. 我在文章下重复了博客。 So if i have 5articles under every is blog post(i have only one). 因此,如果我每个博客帖子下都有5个文章(我只有一个)。 Is there any possible way to show only one blog and then articles? 有没有可能只显示一个博客然后显示文章的方法?

Thanks 谢谢

Most people will tell you to foreach over your outer table, but that is terrible. 大多数人会告诉你在外面的桌子前走一走,但这太可怕了。 Try using the Underscore library http://brianhaveri.github.com/Underscore.php/#groupBy on your result array, as I show here: 尝试在结果数组上使用Underscore库http://brianhaveri.github.com/Underscore.php/#groupBy ,如下所示:

http://codebyjeff.com/blog/2012/08/no-more-machine-gunning-use-underscore-php http://codebyjeff.com/blog/2012/08/no-more-machine-gunning-use-underscore-php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM