そうですね、これは特に熟練していません。これは実際には初めての結合クエリです。私はできるだけ多くの詳細を与えるつもりですが、おそらくフライパンのような顔をしてあなたのほとんどを打つだろうが、それは私のナッツをやっている!クエリに別のデータベース結合を追加すると一部の行が表示されなくなる
私はcodeigniterに書き込もうとしているブログのクエリに問題があります。投稿のための2つの結合を持つクエリを設定しました.3つのテーブルを使用してカテゴリを作成しました。投稿、カテゴリ、posts_categoriesです。コメントテーブルにも参加しようとしています。
これは私が作ってきた一般的なポストの両方を表示する私のモデルのコードです:
$this->db->select('posts.id,
posts.title,
posts.slug,
posts.content,
posts.author,
posts.date,
posts.time,
posts.tags,
posts.status,
GROUP_CONCAT(categories.name SEPARATOR \'-\') AS categories
');
$this->db->group_by(array('posts.id'));
$this->db->from('posts');
$this->db->join('posts_categories', 'posts_categories.blog_entry_id = posts.id');
$this->db->join('categories', 'posts_categories.blog_category_id = categories.category_id');
$query = $this->db->get();
return $query->result_array();
これが結果です:
(
[0] => Array
(
[id] => 1
[title] => My first blog post!
[slug] => my-first-blog-post
[content] => This is my first blog post. Don't worry, it's just a test, my real blog won't be this boring, hopefully!
[author] => Joni
[date] => 2012-01-23
[time] => 00:00:00
[tags] => Testing
[status] =>
[categories] => Testing-More Tests-Test
)
[1] => Array
(
[id] => 2
[title] => This is another test-post
[slug] => this-is-another-test-post
[content] => Well you guessed it. another boring test post, enjoy!
[author] => Joni
[date] => 2012-01-23
[time] => 00:00:00
[tags] => Sexy
[status] =>
[categories] => Test
)
)
今とき、私コメントを3番目の結合を実装するようにクエリを変更します。
$this->db->select('posts.id,
posts.title,
posts.slug,
posts.content,
posts.author,
posts.date,
posts.time,
posts.tags,
posts.status,
GROUP_CONCAT(categories.name SEPARATOR \'-\') AS categories,
count(comments.id) as total_comments
');
$this->db->group_by(array('posts.id'));
$this->db->from('posts');
$this->db->join('posts_categories', 'posts_categories.blog_entry_id = posts.id');
$this->db->join('categories', 'posts_categories.blog_category_id = categories.category_id');
$this->db->join('comments', 'comments.post_id = posts.id');
$query = $this->db->get();
return $query->result_array();
私はあなたがここまでそれを作った場合、この
(
[0] => Array
(
[id] => 1
[title] => My first blog post!
[slug] => my-first-blog-post
[content] => This is my first blog post. Don't worry, it's just a test, my real blog won't be this boring, hopefully!
[author] => Joni
[date] => 2012-01-23
[time] => 00:00:00
[tags] => Testing
[status] =>
[categories] => Testing-More Tests-Test
[total_comments] => 3
)
)
で終わるが、申し訳ありませんが、それはとても長いです、とだけしたい場合は、事前に感謝を言います!あなたはLEFT OUTERを使用する必要が
何が問題ですか?どの列が見えませんか? – safarov
2番目の投稿にコメントがない場合は、表示されません。これはあなたの最下位の結果で起こっていることです。 – OneSneakyMofo