我的数据库中有三个表
员工表
问题表
问卷
这是我自己编写的SQL查询,但它产生的输出我不需要
Select q.qid,e.employeeId, q.Question, eq.response
from employee e
cross join Question q
left outer join employeequestionnaire eq on q.Qid= eq.Qid
where e.employeeId = 1
这是输出
Qid EmployeeId Question Response
"1" "1" "Question1" "0"
"1" "1" "Question1" "1"
"2" "1" "Question2" "1"
"2" "1" "Question2" "0"
"3" "1" "Question3" "1"
"4" "1" "Question4" NULL
但我需要这种输出
Qid EmployeeId Question Response
"1" "1" "Question1" "0"
"2" "1" "Question2" "1"
"3" "1" "Question3" "1"
"4" "1" "Question4" NULL
我是SQL的新手,抱歉我的示例查询..
答案 0 :(得分:0)
假设employeequestionnaire
有employeeID字段,则不需要employee
表。我想你可以做到:
Select q.qid, eq.employeeId, q.Question, eq.response
from Question q left outer join
employeequestionnaire eq
on q.Qid= eq.Qid and
eq.employeeId = 1
请注意,我将where
子句移到了on
子句中。使用left outer join
时,有时需要这样做。
如果您想要包含该表中员工的信息,您将不使用cross join
:
Select q.qid, eq.employeeId, q.Question, eq.response
from Question q left outer join
employeequestionnaire eq
on q.Qid= eq.Qid and
eq.employeeId = 1 left outer join
employees e
on e.EmployeeID = eq.EmployeeID