Select count from another table to each row in result rows

前端 未结 5 695
北恋
北恋 2021-02-08 23:15

Here are the tables:

CREATE TABLE [dbo].[Classes](
    [ClassId] [int] NOT NULL,
    [ClassName] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Classes] PRIMARY KEY CL         


        
5条回答
  •  隐瞒了意图╮
    2021-02-08 23:36

    You mean something like this?

    SELECT C.[ClassName], COUNT(*) AS 'Number of Students'
    FROM [dbo].[Classes] AS C
        INNER JOIN [dbo].[Students] AS S ON S.[ClassId] = C.[ClassId]
    GROUP BY C.[ClassName]
    

提交回复
热议问题