I've three tables: posts, post_tags and tags. One publish might have many tags, and something tag can fit in with many posts. Due to that lots of-to-many relationship, I designed a post_tags table. It's two fields: p_id and t_id. They are both foreign secrets towards the posts table and tags table correspondingly. Now, after i run my PHP technique to get the most recent posts, I wish to also retrieve the tags owned by that publish in a single query. Only for reference, here's individuals three tables:

posts

| p_id | c_id | u_id |   title   |     body    |      published      |
----------------------------------------------------------------------
|  1   |  1   |   1  | first post| lorem ipsum | 2012-01-27 18:37:47 |

publish_tags

| p_id | t_id |
---------------
|  1   |  3   |

tags

| t_id |     name    |     slug    |
------------------------------------
|  3   | programming | programming |

Here's the PHP code I am using now to obtain the latest posts with no tags:

public function getLatestPosts()
{
    $query = $this->db->query('SELECT title, clean_title, body, published FROM posts ORDER BY published DESC');
    $blogPosts = array();
    foreach ($query->result() as $row)
    {
        $blogPosts[] = array('title' => $row->title,
                             'clean_title' => $row->clean_title,
                             'body' => $row->body,
                             'published' => $row->published);
    }

    return $blogPosts;
}

How do i adapt my query to obtain the title and slug from the tags owned by each publish?

Thank you for any help!

Implicit join:

SELECT title, clean_title, body, published, name, slug
FROM posts, posts_tags, tags
WHERE posts.p_id=posts_tags.p_id AND posts_tags.t_id=tags.t_id
ORDER BY published DESC

Explicit join:

SELECT title, clean_title, body, published, name, slug
FROM posts
LEFT JOIN posts_tags ON posts.p_id=posts_tags.p_id
LEFT JOIN tags ON posts_tags.t_id=tags.t_id
ORDER BY published DESC

It's refreshing to determine a proper, stabilized database schema at least.

You'll most likely would like to build them out right into a separate array becasue it is many-to-many.

public function getLatestPosts()
{
    $query = $this->db->query('SELECT p_id, title, clean_title, body, published FROM posts ORDER BY published DESC');
    $blogPosts = array();
    foreach ($query->result() as $row)
    {
        $blogPosts[] = array('title' => $row->title,
                             'clean_title' => $row->clean_title,
                             'body' => $row->body,
                             'published' => $row->published,
                             'tags' => $this->getPostTags($row->p_id);
    }
    return $blogPosts;
}

public function getPostTags($pid)
{
    $query = $this->db->query('SELECT name, slug FROM tags INNER JOIN post_tags on tags.t_id = post_tags.t_id WHERE post_tags.p_id = ' . $pid);
    $postTags = array();
    foreach ($query->result() as $row)
    {
        $postTags[] = array('name' => $row->name,
                             'slug' => $row->slug);
    }
    return $postTags;
}