[英]How do I combine two queries and get data into one row?
I have a situation here to get data(ie fname and lname) from two different table comparing their jobid, deliverymanid, pickupmanid and employeeid from job and employee table and combine it in one row. 我在这里有一种情况,要从两个不同的表中获取数据(即fname和lname),将它们的jobid,employeemanid,pickupmanid和employeeid从job和employee表中进行比较,并将其合并为一行。
This is the job table 这是工作表
jobid pickupmanid deliverymanid
----- ----------- -------------
1 1 2
2 2 2
This is the employee table 这是员工表
employeeid fname lname
---------- ----------- -------------
1 ABC XYZ
2 LMN OPR
Here pickupmanid and deliverymanid act as a foreign keys for job table referring to employeeid in employee table. 在这里,pickupmanid和deliverymanid充当作业表的外键,引用了employee表中的employeeid。
You could join the job
table on the employee
table twice - once for the picker-upper and once for the deliveryman: 您可以将
job
表连接到employee
表上两次-一次用于上层拣选,一次用于送货员:
SELECT j.jobid,
p.fname AS pickup_fname, p.lname AS pickup_lname,
d.fname AS delivery_fname, d.lname AS delivry_lname
FROM job j
JOIN employee p ON p.employeeid = j.pickupmanid
JOIN employee d ON d.employeeid = j.deliverymanid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.