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)

基本的に、replytoフィールドが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

これに一日を費やした後、私のコードは次のようになりました。

  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 に答える
1

これらはSQLの基本です。あなたは本当にいくつかのSQLを学ぶべきです。学ぶのは簡単です、あなたはそれに30分を費やすことができました、そしてそれは非常に有益でしょう。

まず、特定のユーザーの投稿は次のようになります。

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 に答える