简体   繁体   English

Codeigniter-数据库-> join()双结果合并

[英]Codeigniter - db -> join () double results merger

I am building a NOTE system. 我正在构建一个NOTE系统。

I am having multiple tables (notes, tags, ...) and I am joining tags to my main notes with $this -> db -> join(); 我有多个表(笔记,标签等),并通过$ this-> db-> join();将标签加入到我的主要笔记中。 to get everything into one object. 将所有东西变成一个对象。

When there are 2 or more tags for one note, then I get two or more rows with only different TAGS. 当一个音符有2个或更多标签时,我得到两行或更多行只有不同的标签。 The rest is the same. 其余部分相同。 I need to merge it to have only one note entry. 我需要将其合并为只有一个注释条目。

$this -> db -> where ('user', USER_ID);
$this -> db -> join ('tags', 'tags.note_id = note.id', 'inner');
$query = $this->db->get('notes');

There may be also other tables with same character as TAGS, for example places. 可能还有其他具有与TAGS相同字符的表,例如场所。 There may be more than one place for a note. 笔记可能有多个地方。

How do I proceed from now? 从现在开始我该如何进行? I would like to have one object NOTE with parameters such as note_id, note_text, and join TAGS to it and probably if more than ONE tag, then OBJECT PARAMETER = ARRAY containing all the NOTES. 我想拥有一个对象NOTE,该对象具有诸如note_id,note_text之类的参数,并可能将TAGS与其连接,如果可能超过一个标签,则OBJECT PARAMETER = ARRAY包含所有NOTES。

  1. How to achieve that? 如何实现呢?
  2. Is that actually good idea for further development to have it in one object? 将其整合到一个对象中实际上是个好主意吗? Or should I go foreach and list all the tags for each of the rows? 还是我应该去foreach并列出每一行的所有标签?
  3. When somebody is filtering according to the tags, where & how should I store one's filtering? 当有人根据标签进行过滤时,我的过滤条件应该存储在哪里? I am so far using $this -> session; 到目前为止,我正在使用$ this-> session;

Thank you, Jakub 谢谢雅库布

You might want to use MySQL's GROUP_CONCAT() 您可能要使用MySQL的GROUP_CONCAT()

$this->db->select('n.*, GROUP_CONCAT(t.name)', false)
         ->from('notes n')->join('tags t', 't.note_id = n.id', 'inner')
         ->where('n.user', USER_ID)->get();

I used t.name but whatever the field name it is, you get the point. 我使用了t.name但是无论它是什么字段名称,您都可以t.name

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

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