[英]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。
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.