SQL: Get all records from one table AND a count of records from a second table?

前端 未结 3 1810
我寻月下人不归
我寻月下人不归 2021-01-12 13:15

Say there are two tables:

TABLE A

messageID / Message                     / More..
1         / This is the first message   / Etc..
2         


        
3条回答
  •  情深已故
    2021-01-12 13:56

    You can use CTE for the same.

    ;WITH CTE_MessageCount (MessageId, Count)
    AS
    (
    SELECT MessageId, Count(*) FROM TableB GROUP BY MessageId
    ) 
    
    SELECT A.*, T.*
    FROM tableA A JOIN CTE_MessageCount T ON A.messageID = T.MessageID
    

提交回复
热议问题