I have three table in my database
Employee Table
Question Table
Questionnaire
This is the SQL Query that I write on my own, but it is producing an output that I do not need
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
This is the output
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
But I need this kind of Output
Qid EmployeeId Question Response
"1" "1" "Question1" "0"
"2" "1" "Question2" "1"
"3" "1" "Question3" "1"
"4" "1" "Question4" NULL
I am a newbie in SQL, sorry for my sample query..
Assuming the employeequestionnaire
has the employeeID field, you don't need the employee
table. I think you can just do:
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
Notice that I moved the where
clause into an on
clause. This is sometimes necessary when using a left outer join
.
If you wanted to include information about employees from that table, you would use not use a 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.