Welcome Guest, Not a member yet? Register   Sign In
How to combine two rows into one in mysql query?
#5

[eluser]danmontgomery[/eluser]
GROUP_CONCAT() will concat a field based on the GROUP BY statement, but it will be a string. There are no arrays in mysql, so you will have to explode the result.

Code:
$query = $this->db
    ->select('posts.post_id, posts.title, GROUP_CONCAT(tags.title SEPARATOR ",") AS tags', NULL, FALSE)
    ->join('tags', 'tags.post_id = posts.post_id', 'left')
    ->where('posts.post_id', 5)
    ->group_by('posts.post_id')
    ->get('posts');

if($query && $query->num_rows())
{
    $post = $query->row();
    $post->tags = explode(',', $post->tags);
}

http://dev.mysql.com/doc/refman/5.0/en/g...oup-concat


Messages In This Thread
How to combine two rows into one in mysql query? - by El Forum - 07-15-2011, 05:52 AM
How to combine two rows into one in mysql query? - by El Forum - 07-15-2011, 09:48 AM
How to combine two rows into one in mysql query? - by El Forum - 07-15-2011, 10:02 AM
How to combine two rows into one in mysql query? - by El Forum - 07-15-2011, 10:21 AM
How to combine two rows into one in mysql query? - by El Forum - 07-19-2011, 06:59 AM



Theme © iAndrew 2016 - Forum software by © MyBB