履歴書:
- tbl.Classの複数の教師
- tbl.Courseのシングル教師
- 結果は、tbl.Courseの各レコードに対してtbl.Classから1人の教師を与えるはずです。
私のSQLの解釈:
DECLARE @class TABLE ([id] int, [teacher] nvarchar(20))
DECLARE @course TABLE ([id] int, [course] nvarchar(20), [teacher_course] nvarchar(20))
INSERT INTO @class ([id], [teacher]) VALUES (1, 'Teacher 1')
INSERT INTO @class ([id], [teacher]) VALUES (2, 'Teacher 2')
INSERT INTO @class ([id], [teacher]) VALUES (3, 'Teacher 3')
INSERT INTO @class ([id], [teacher]) VALUES (4, 'Teacher 4')
INSERT INTO @class ([id], [teacher]) VALUES (5, 'Teacher 1')
INSERT INTO @course ([id], [course], [teacher_course]) VALUES (1, 'Course 1', 'Teacher 1')
INSERT INTO @course ([id], [course], [teacher_course]) VALUES (2, 'Course 2', 'Teacher 4')
SELECT
[co].[id],
[co].[course],
[cl].[teacher]
FROM @course [co]
INNER JOIN @class [cl] ON [co].[teacher_course] = [cl].[teacher]
GROUP BY
[co].[id],
[co].[course],
[cl].[teacher]
その結果:
[id] [course] [teacher]
1 Course 1 Teacher 1
2 Course 2 Teacher 4