如何使用新的连接语法连接SQL Server?

时间:2014-07-23 19:30:49

标签: sql-server

我有一个SQL Select:

SELECT @AnswerGridCorrect = '00'+CASE WHEN Correct = 1 THEN '1' ELSE '0' END 
FROM dbo.Answer
WHERE QuestionId = 9

如果我只知道QuestionUId,我怎样才能从Answer表中获取数据?

CREATE TABLE [dbo].[Question] (
    [QuestionId]       INT              IDENTITY (1, 1) NOT NULL,
    [QuestionUId]      UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL
);

CREATE TABLE [dbo].[Answer] (
    [AnswerId]    INT              IDENTITY (1, 1) NOT NULL,
    [QuestionId]  INT              NOT NULL
);

4 个答案:

答案 0 :(得分:1)

SELECT @AnswerGridCorrect = '00'+CASE WHEN Answer.Correct = 1 THEN '1' ELSE '0' END 
FROM dbo.Answer
JOIN dbo.Question
ON Question.QuestionId = Answer.QuestionId
WHERE Question.QuestionUId = @QuestionUId

答案 1 :(得分:1)

不太确定你的意思,但这样的事情呢?

select [your Columns Here]
from Question q
join Answer a on a.QuestionID = q.QuestionID

答案 2 :(得分:1)

SELECT *
FROM [dbo].[Question] q
INNER JOIN [dbo].[Answer] a
  ON q.QuestionId = a.QuestionId
WHERE q.QuestionUId = KnownQuestionUId

答案 3 :(得分:0)

SELECT @AnswerGridCorrect = '00'+CASE WHEN a.Correct = 1 THEN '1' ELSE '0' END 
FROM dbo.Answer a
INNER JOIN dbo.Question q
    ON a.QuestionId = q.QuestionId
WHERE q.QuestionUId = @myuid

你刚刚打电话给内心联盟'并使用''用于选择要加入的字段的语句