我有3個表。將行連接成一個並根據編號對行進行計數
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
結果picture_name
正好趕上1 picture_name
即使id_post
有多於1 picture_name
,comment_count
顯示金額picture_name
* comment_count
。
請任何人都可以幫助我解決我的問題?
工作,謝謝 –