私は3つのテーブルを持っています。私が作りたい行を1に連結し、IDに基づいて行を数えよう
post_table
id_post | post_text
1 | great view
2 | happy breakfast
3 | good night everybody
comment_table
id_comment | comment_text | id_post
1 | that's amazing | 1
2 | of course, the best | 1
3 | wish me there | 1
4 | yes, happy breakfast | 2
5 | hehe | 2
attach_picture
id_picture | picture_name | id_post
1 | pict_1 | 1
2 | pict_2 | 1
このようなビューを作成することができ、クエリ:
id_post | post_text | picture_name | comment_count
1 | great view | pict_1, pict_2 | 3
2 | happy breakfast | null | 2
3 | goodnight everybody | null | 0
私はこのようなクエリを記述します。
select a.id_post, a.post_text, b.picture_name, count(c.id_comment) as comment_count
from post_table left join
attach_picture
on a.id_post=b.id_post left join
comment_table c
on a.id_post=c.id_post
group by a.id_post
クエリの結果は次のとおりです。
id_post | post_text | picture_name | comment_count
1 | great view | pict_1 | 6
2 | happy breakfast | null | 2
3 | goodnight everybody | null | 0
結果はわずか1 picture_name
をキャッチpicture_name
ですid_post
であっても、picture_name
以上であり、comment_count
は、 * comment_count
。
私の問題を解決するのに誰も助けてください。
ありがとうございます –