0

大家好,我有这些桌子;

USERS( user_id ,fullname ,username etc.)

POSTS ( post_id, user_id, post, orig_post_id, replyto date)

USER_PROFILE (id, user_id, profile_image_path etc)

例子

USERS (1 ,John Doe ,johndoe etc.),( 2 ,Stack Flow ,stackflow etc.)

POSTS (2, 1, My naame is John doe and i approve this message, 0, 0,sometimestamp),
      (3, 12, My naame is Stack Flow and i approve this message, 0, 2,sometimestamp)

USER_PROFILE (1, 1, ppdjodjf.jpg etc),(2, 2, grsdjodjf.jpg etc)

基本上,如果回复字段为 0,我希望查询输出这个

array('post_id' => 2, 
'user_id' => 1, 
'post' => the post, 
'orig_post_id,' => 0
'replyto,' => 0
username => johndoe,
fullname => John Doe,
profile_image_path => etc)

当它不为零时

array('post_id' => 3, 
'user_id' => 2, 
'post' => Another post, 
'orig_post_id,' => 0
'replyto,' => 2
username => stackflow,
fullname => Stack Flow,
profile_image_path => etc,
'replies' => array(all the replies)
4

2 回答 2

1

这些是 SQL 的基础知识。你真的应该学习一些 SQL。简单易学,花半个小时就可以了,受益匪浅。

首先,给定用户的帖子将是:

select posts.*
from posts
where posts.user_id = '$user_id'

要获取您想要的用户字段,请加入

select posts.*,users.username,users.fullname
from posts
  inner join users where posts.user_id = users.user_id
where posts.user_id = '$user_id'

您应该能够弄清楚如何加入 user_profile 以获取这些字段。

要仅过滤那些没有 orig_post_id 的记录,您可能需要测试为零,或者您可能需要测试 NULL。也许两者兼而有之,因此假设您想同时测试两者:

where posts.user_id = '$user_id'
  and (orig_post_id = 0 or orig_post_id is null)
于 2013-01-10T14:14:37.053 回答
1

在花了一整天的时间之后,我的代码最终看起来像这样。

  public function get_connected_post()
    {
        $user_id = $this->session->userdata('user_id');

        $sql = "SELECT  p.*,up.fullname,u.username,upi.file_path_thumb,IF(hi5c.hi5_count is NULL,'0',hi5c.hi5_count) AS count_hi5,
                        IF(brn.branch_count is NULL,'0',brn.branch_count) AS count_branch, IF(rply.reply_count is NULL,'0',rply.reply_count) AS count_reply,
                        IF((SELECT COUNT(*) FROM post_highfives ph WHERE ph.user_id = $user_id AND ph.post_id = p.post_id),'1','0') AS count_is_hi5ed,
                        IF((SELECT COUNT(*) FROM posts pt WHERE pt.user_id = $user_id AND pt.is_branch_of_id = p.post_id),'1','0') AS count_is_branched
                FROM    (
                         SELECT  user_id
                         FROM    user_followers
                         WHERE   follower_id = $user_id
                         UNION ALL
                         SELECT  $user_id
                        ) uf
                JOIN    posts p
                ON      p.user_id = uf.user_id
                JOIN   user_profile up
                ON     up.user_id = p.user_id
                JOIN user_profile_images upi
                ON     upi.image_id = up.profile_image_id
                JOIN users u
                ON     u.user_id = p.user_id
                LEFT OUTER JOIN (SELECT ph.post_id, count(*) AS hi5_count
                                 FROM post_highfives ph
                                 GROUP BY ph.post_id) hi5c
                ON p.post_id = hi5c.post_id
                LEFT OUTER JOIN (SELECT pst.post_id,pst.is_branch_of_id, count(*) AS branch_count
                                 FROM posts pst
                                 GROUP BY pst.is_branch_of_id) brn
                ON p.post_id = brn.is_branch_of_id
                LEFT OUTER JOIN (SELECT pst.post_id,pst.reply_to, count(*) AS reply_count
                                 FROM posts pst
                                 GROUP BY pst.reply_to) rply
                ON p.post_id = rply.reply_to
                ORDER BY p.post_date DESC";


        $query = $this->db->query($sql);
        if ($query) {
            $result = array();
                foreach($query->result_array() as $r){
                    $branch_id = $r['is_branch_of_id'];
                    if($branch_id != 0){
                        $branch_array = $this->branch_query($this->postid_return_user_id($branch_id),$branch_id);
                    }else{
                        $branch_array = array();
                    }
                    $result[] = array_merge((array)$branch_array, (array)$r);
                }
            return $result;
        }
         else {
            return false;
        }


    }

我知道这看起来与我提出的问题不同,但我试图简化我实际在做什么。我不认为提出一个说明这个问题的问题会让我到任何地方,尽管简单地提出这个问题仍然没有让我到任何地方没有人理解我哈哈。无论如何,对于我的问题,相关部分是代码的最后一点,我在其中设置了一个条件,如果 branch_id 不为零,则获取一些数组数据,如果它返回一个空数组。我将数组与查询结果合并在一起。现在我必须考虑如何简化这一点。

于 2013-01-11T02:28:55.490 回答