帮助基本的SQL查询

时间:2011-04-30 15:02:32

标签: sql

此示例已简化。我有以下设计: http://img835.imageshack.us/i/designyi.jpg/

我已经插入了这样的测试数据:

INSERT INTO Period VALUES ('Survey for 2011', 1)

INSERT INTO EvalQuestion VALUES('How do...')
INSERT INTO EvalQuestion VALUES('How many...')
INSERT INTO EvalQuestion VALUES('Which is....')

INSERT INTO EvalQuestion_Period VALUES (1, 1)
INSERT INTO EvalQuestion_Period VALUES (1, 2)
INSERT INTO EvalQuestion_Period VALUES (1, 3)

INSERT INTO Employee VALUES ('Peter', 'Smith')
INSERT INTO Employee VALUES ('Britney', 'Spears')

INSERT INTO EvalAnswer VALUES(1,'Fine',1)
INSERT INTO EvalAnswer VALUES(2,'45',1)
INSERT INTO EvalAnswer VALUES(3,'I don´t know',1)
INSERT INTO EvalAnswer VALUES(1,'Fine again',2)
INSERT INTO EvalAnswer VALUES(2,'45 again',2)
INSERT INTO EvalAnswer VALUES(3,'I don´t know again',2)

我运行以下查询以获得Peter的问题和答案:

Select Name, Answer 
from EvalQuestion eq
LEFT JOIN EvalQuestion_Period eqp ON eq.Id = eqp.FK_EvalQuestion
LEFT JOIN EvalAnswer ea ON ea.FK_EvalQuestion_Period = eqp.Id
where ea.FK_Employee = 1

结果集:

Name           Answer
-----------------------
How do...      Fine
How many...    45
Which is....   I don´t know

这看起来不错。如果我删除其中一个Peters Answers:

Delete from EvalAnswer where ID= 1

运行相同的查询我只得到两行,比如这个

Name           Answer
-----------------------
How many...    45
Which is....   I don´t know

我在结果集中需要我的问题,即使它没有答案,如下:

Name           Answer
-----------------------
How do....     NULL
How many...    45
Which is....   I don´t know

任何提示?感谢

2 个答案:

答案 0 :(得分:0)

如果您想要从原始表中返回1行或更多行,则可以使用外部联接。所以你的查询将是:

Select Name, Answer 
    from EvalQuestion eq
        LEFT JOIN EvalQuestion_Period eqp ON eq.Id = eqp.FK_EvalQuestion
        LEFT OUTER JOIN EvalAnswer ea ON ea.FK_EvalQuestion_Period = eqp.Id
    where ea.FK_Employee = 1

当没有相应的记录时,这将返回EvalAnswer值的空值,否则将与LEFT JOIN完全相同。

答案 1 :(得分:0)

您的“左连接”实际上是LEFT OUTER JOIN(尽管有其他答案):LEFTRIGHT隐含连接类型,OUTER是可选的

当您使用WHERE ea.FK_Employee = 1时,您将其更改为INNER JOIN,因为您不允许缺少行。您需要先过滤(即在连接之前限制EvalAnswer上的行)。这是因为在JOIN..ON逻辑上处理了WHERE。

使用派生的过滤表尝试此操作:

Select Name, Answer 
from EvalQuestion eq
LEFT JOIN
EvalQuestion_Period eqp ON eq.Id = eqp.FK_EvalQuestion
LEFT JOIN
       (SELECT * FROM EvalAnswer
        where FK_Employee = 1
        ) ea ON ea.FK_EvalQuestion_Period = eqp.Id

或者在ON状态下过滤:

 Select Name, Answer 
from EvalQuestion eq
    LEFT JOIN
    EvalQuestion_Period eqp ON eq.Id = eqp.FK_EvalQuestion
    LEFT JOIN 
    EvalAnswer ea ON ea.FK_EvalQuestion_Period = eqp.Id AND ea.FK_Employee = 1