DECLARE @Assignments TABLE
(
Id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(30) NOT NULL
)
DECLARE @AssignmentNotes TABLE
(
Id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
AssignmentId INT NOT NULL,
Note VARCHAR(MAX)
)
INSERT INTO @Assignments(Name) VALUES('Biology')
INSERT INTO @Assignments(Name) VALUES('Chemistry')
INSERT INTO @AssignmentNotes (AssignmentId, Note) VALUES(1, 'Studies on DNA')
INSERT INTO @AssignmentNotes (AssignmentId, Note) VALUES(1, 'Evolution notes from Darwin')
SELECT
A.*,
CASE WHEN COUNT(AN.Id) > 0 THEN 1 ELSE 0 END AS HasNotes
FROM
@Assignments AS A
LEFT JOIN
@AssignmentNotes AS AN
ON A.Id = AN.AssignmentId
GROUP BY
A.Id,
A.Name
對於SQL Server的問題,如果您可以讓人們知道您使用的是哪個版本,那麼它會有所幫助 - 某些語言功能在舊版本中不可用。謝謝:) – Meff 2010-07-13 10:32:17