我有一个名为 schoolDB 的数据库和 2 个数据库表,
student
和education
创建学生表:
USE [schoolDB]
GO
/****** Object: Table [dbo].[tblStudent] Script Date: 09/22/2013 17:30:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tblStudent](
[STUDENTNUMBER] [varchar](50) NOT NULL,
[STUDENTNAME] [varchar](50) NULL,
[EDUCATIONID] [varchar](50) NULL,
CONSTRAINT [PK_tblStudent] PRIMARY KEY CLUSTERED
(
[STUDENTNUMBER] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
创建教育表:
USE [schoolDB]
GO
/****** Object: Table [dbo].[tblEducation] Script Date: 09/22/2013 17:31:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tblEducation](
[EDUCATIONID] [varchar](50) NOT NULL,
[STUDENTNUMBER] [varchar](50) NULL,
[INSTITUTIONNAME] [varchar](50) NULL,
[COURSENAME] [varchar](50) NULL,
[GRADE] [varchar](50) NULL,
[YEAROFLEAVING] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
这是数据的截图:
我希望能够找到每个去过一个名为的机构Secondary School
并且有另一个带有课程名称的教育记录的人like
biol
。不仅限于生物学,我想找到所有的科学,所以我需要放多个类似的陈述。
我试过这个:
SELECT COUNT(*) AS 'Our Students',
DTOurStudents.STUDENTNAME
FROM (SELECT TOP 2 TBLSTUDENT.STUDENTNUMBER,
TBLSTUDENT.STUDENTNAME,
TBLEDUCATION.INSTITUTIONNAME,
TBLEDUCATION.COURSENAME
FROM TBLEDUCATION
INNER JOIN TBLSTUDENT
ON TBLEDUCATION.STUDENTNUMBER = TBLSTUDENT.STUDENTNUMBER
WHERE TBLEDUCATION.INSTITUTIONNAME LIKE '%Secondary School%')
DTOurStudents
GROUP BY DTOurStudents.STUDENTNAME
SQL FIDDLE: http ://sqlfiddle.com/#!3/666f8/2