我有一种情况来从两个不同的表中获取数据(即fname,lname和count(jobid)),比较他们的jobid,deliverymanid,pickupmanid和employeeid来自job和employee表,并将它组合成一行。
这是工作表
jobid pickupmanid deliverymanid ----- ---------------- ------------------ 1 1 2 2 2 2 3 1 1
这是员工表
employeeid fname lname ------------ ----------- ------------- 1 ABC XYZ 2 LMN OPR
这就是我应该如何获得输出
employeeid totalpickupjobs totaldeliveryjobs fname lname ---------- --------------- ----------------- ----------- ----------- 1 2 1 ABC XYZ 2 1 2 LMN OPR
答案 0 :(得分:1)
试试这个:
WITH x AS (SELECT 1 AS jobid,1 AS pickupmaid, 1 AS delivery_manid FROM dual UNION ALL
SELECT 2 AS jobid,2 AS pickupmaid, 2 AS delivery_manid FROM dual UNION ALL
SELECT 3 AS jobid,1 AS pickupmaid, 1 AS delivery_manid FROM dual ),
y AS (SELECT 1 AS employeeid,'ABC' AS fname, 'XYZ' AS lname FROM dual UNION ALL
SELECT 2 AS employeeid,'LMN' AS fname, 'OPR' AS lname FROM dual )
SELECT y.employeeid as employee_id,
count(pickupmaid) as totalpickup,
count(delivery_manid) as totaldelivery,
y.fname as firstname,
y.lname as lastname
FROM y FULL OUTER JOIN x ON X.pickupmaid=y.employeeid group by y.employeeid, y.fname, y.lname;
答案 1 :(得分:1)
答案 2 :(得分:0)
使用mysql count
,inner join
或group by
: -
select *,count(job.deliverymanid) from Job inner
join Pickupman on Job.pickupmanid=Pickupman.pickupmanid
group by deliverymanid