在多个表上使用一对多构造MySQL查询时遇到问题

时间:2010-10-21 22:14:03

标签: php activerecord codeigniter mysql

我有一个博客风格的应用程序,允许用户使用主题标记每个帖子。我将这些数据保存在三个单独的表中:帖子(用于实际的博客帖子),主题(用于各种标签)和posts_topics(存储两者之间关系的表格)。

为了保持MVC结构(我使用Codeigniter)尽可能干净,我想运行一个MySQL查询,该查询抓取所有后期数据和相关主题数据,并将其返回到一个数组或对象中。到目前为止,我没有运气。

表结构如下:

Posts
+--------+---------------+------------+-----------+--------------+---------------+
|post_id | post_user_id  | post_title | post_body | post_created | post_modified |
+--------+---------------+------------+-----------+--------------+---------------+
|     1  |    1          | Post 1     |  Body 1   |  00-00-00    |  00-00-00     |
|     2  |    1          | Post 1     |  Body 1   |  00-00-00    |  00-00-00     |
+--------+---------------+------------+-----------+--------------+---------------+

// this table governs relationships between posts and topics
Posts_topics
+--------------+---------------------+-------------------------+-----------------+
|post_topic_id | post_topic_post_id  | post_topic_topic_id | post_topic_created  | 
+--------------+---------------------+-------------------------+-----------------+
|     1        |    1                | 1                   |         00-00-00    |  
|     2        |    1                | 2                   |         00-00-00    |
|     3        |    2                | 2                   |         00-00-00    |  
|     4        |    2                | 3                   |         00-00-00    |   
+--------------+---------------------+-------------------------+-----------------+

Topics
+---------+-------------+-----------+----------------+
|topic_id | topic_name  | topic_num | topic_modified |
+---------+-------------+-----------+----------------+
|     1   |  Politics   | 1         | 00-00-00       |
|     2   |  Religion   | 2         | 00-00-00       |
|     3   |  Sports     | 1         | 00-00-00       |
+---------+-------------+-----------+----------------+

我尝试了n次成功的简单查询:

select * from posts as p inner join posts_topics as pt on pt.post_topic_post_id = post_id join topics as t on t.topic_id = pt.post_topic_topic id

我也尝试过使用GROUP_CONCAT,但这给我带来两个问题:1)我需要来自主题的所有字段,而不仅仅是名称,以及2)我的MySQL中有一个小故障所以所有GROUP_CONCAT数据都返回为BLOB(见here)。

我也愿意听取任何建议,我会运行两个查询并尝试为每个结果构建一个数组;我尝试使用下面的代码,但失败了(此代码还包括加入用户表,这也很好,以保持这一点):

    $this->db->select('u.username,u.id,s.*');
    $this->db->from('posts as p');
    $this->db->join('users as u', 'u.id = s.post_user_id');
    $this->db->order_by('post_modified', 'desc');
    $query = $this->db->get();
    if($query->num_rows() > 0)
    {
        $posts = $query->result_array();
        foreach($posts as $p)
        {
            $this->db->from('posts_topics as p');
            $this->db->join('topics as t','t.topic_id = p.post_topic_topic_id');
            $this->db->where('p.post_topic_post_id',$p['post_id']);
            $this->db->order_by('t.topic_name','asc');
            $query = $this->db->get();
            if($query->num_rows() > 0)
            {
                foreach($query->result_array() as $t)
                {
                    $p['topics'][$t['topic_name']] = $t;
                }
            } 
        }
        return $posts;
    }

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

此查询应该可以解决问题。只需将*更改为您希望的字段列表,这样您就不会在每次运行查询时提取过多的数据。

Select
  *
FROM
  posts,
  post_topics,
  topics
WHERE
  post_topic_topic_id = topic_id AND
  post_topic_post_id = post_id
 ORDER BY
 post_id, topic_id;

Select
  *
FROM
  posts,
  post_topics,
  topics,
  users
WHERE
  post_topic_topic_id = topic_id AND
  post_topic_post_id = post_id AND
  post_user_id = user_id
 ORDER BY
 post_id, topic_id;

答案 1 :(得分:1)

圣牛,你可以做到!看到它有助于帮助。从来不知道,试试这个 选择   POST_ID,   GROUP_CONCAT(DISTINCT topic_name)作为名称 从   帖子   post_topics,   话题 哪里   post_topic_topic_id = topic_id AND   post_topic_post_id = post_id

GROUP BY   POST_ID;

你得到了 1,'政治,relligion' 2,'体育,relligion'