Richtig, ich bin nicht besonders geschickt darin, dies ist in der Tat meine erste Join-Abfrage, also sei sanft. Ich werde so viel Details wie möglich geben, da es wahrscheinlich die meisten von euch im Gesicht treffen wird wie eine Bratpfanne, aber es macht meine Nuss rein!Das Hinzufügen eines weiteren Datenbank-Joins zu meiner Abfrage führt dazu, dass einige Zeilen nicht angezeigt werden.
Ich habe Probleme mit einer Abfrage für einen Blog, den ich in Codeigniter schreiben möchte. Ich hatte eine Abfrage mit 2 Joins für Posts eingerichtet und ihre Kategorien verwenden drei Tabellen: Posts, Kategorien und posts_categories Jetzt versuche ich auch, mich meiner Kommentartabelle anzuschließen, um eine Zählung durchzuführen.
Dies ist der Code in meinem Modell, das sowohl die allgemeinen Beiträge zeigt i up gemacht haben:
$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();
das ist das Ergebnis:
(
[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
)
)
Jetzt, als ich Ändern Sie die Abfrage so, dass der dritte Join für Kommentare wie folgt implementiert wird:
$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();
ich am Ende mit diesem
(
[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
)
)
Wenn Sie es bis hierher geschafft haben, ist leider es so lange, und möchte nur sagen, Dank im Voraus!
prost joni
Was ist das Problem? Welche Reihe sieht nicht? – safarov
Wenn Ihr zweiter Beitrag keinen Kommentar enthält, wird er nicht angezeigt. Dies ist, was in Ihrem unteren Ergebnis geschieht. – OneSneakyMofo