嗨我有这个数据库表,我有例如
-transaction number- pk
-id_number- fk from employees table
-request-
-department_request_status-
-department_oic- fk from id_number of employees table again
-headoffice
-headoffice_oic- fk from id_number of employees table again
我可以举例如左,然后在表格中显示三个id所有者的名字吗?
例如
SELECT
table.ID_NUM,
table.DEP_OIC,
table.HeadOffc_OIC
employees.LNAME, ===>select this for ID_NUM,DEP_OIC, and HeadOffc_OIC
employees.FNAME ===>select this for ID_NUM,DEP_OIC, and HeadOffc_OIC
FROM leave_application
LEFT JOIN employees
WHERE leave_application.ID_NUM = employees.ID_NUM
LEFT JOIN employees
WHERE leave_application.DEP_OIC = employees.ID_NUM
我该怎么做呢?可能吗?或者我必须将它们分成三个表?
答案 0 :(得分:0)
你可以将每个EmployeeID(id_number,department_oic,headoffice_oic)分别与leave_application表连接,并将它们作为一个表格查看,然后使用leave_application将所有结果连接起来,现在你将所有三个所有者名称都放在一行:
select leave_application.tranid, HeadOffFName, HeadOffLName, DepFName, DepLName, FName, LName from leave_application
LEFT JOIN
(select TranID, FName as HeadOffFName, LName as HeadOffLName from
Employee inner join leave_application
on Employee.EmpID = leave_application.HeadOffc_OIC)Head
ON leave_application.TranID = Head.tranid
left join
(select TranID, FName as DepFName, LName as DepLName from
Employee inner join leave_application
on Employee.EmpID = leave_application.DEP_OIC)Dep
ON leave_application.TranID = Dep.tranid
left join
(select TranID, FName, LName from
Employee inner join leave_application
on Employee.EmpID = leave_application.ID_NUM)Office
ON leave_application.TranID = Office.tranid