T-SQL Select and count from different table?

前端 未结 2 1226
南方客
南方客 2020-12-19 02:47

I have a table (Threads) containing a field (id). I would like to select every row from Threads, as well as the number of rows in the

相关标签:
2条回答
  • 2020-12-19 03:22
    SELECT t.id, COUNT(p.thread)
    FROM Threads AS t
        LEFT OUTER JOIN Posts AS p
            ON t.id = p.thread
    GROUP BY t.id
    
    0 讨论(0)
  • 2020-12-19 03:29

    Sure - something like this?

    SELECT 
        t.ThreadID,
        (SELECT COUNT(*) FROM dbo.Posts p WHERE p.ThreadID = t.ThreadID)
    FROM
        dbo.Threads t
    
    0 讨论(0)
提交回复
热议问题