Let's say that there are two tables:
TABLE A
messageID / Message / More.. 1 / This is the first message / Etc.. 2 / This is the second message / Etc.. 3 / This is the third message / Etc..
TABLE B
commentID / messageID / Comment 1 / 2 / This is a comment to the second message 2 / 2 / This is another comment to the second message 3 / 3 / This is a comment to the third message
A table relationship is a messageID field.
I need one query that generates such results when I get ALL the fields from table A and count the number of comments for each message from table B, for example:
messageID / Message / More... / CommentCount 1 / This is the first message / etc... / 0 2 / This is the second message / etc... / 2 3 / This is the third message / etc... / 1
I tried something like this:
SELECT tableA.*, count(commentID) as commentcount FROM tableA LEFT JOIN tableB ON tableA.messageID = tableB.messageID GROUP BY messageID
but that will not work. Any ideas? It seems like this should be possible to do in one request. I am using MSSQL. Thanks for any help.
source share