gpt4 book ai didi

mysql - 如何在 phpmyadmin 中加入表

转载 作者:行者123 更新时间:2023-11-29 19:29:21 24 4
gpt4 key购买 nike

这是我需要合并的两个查询:

第一个查询:

SELECT emp.name, count(rej.employee_ic) as 'rejected leave'
FROM employee as emp
LEFT OUTER JOIN rejectedleave as rej
ON emp.ic_no = rej.employee_ic
GROUP BY emp.`ic_no`

第二个查询:

SELECT emp.name, count(app.employee_ic) as 'approved leave'
FROM employee as emp
LEFT OUTER JOIN approvedleave as app
ON emp.ic_no = app.employee_ic
GROUP BY emp.`ic_no`

输出:

第一个查询: first query output image

第二个查询: second query output image

我想将这两个表合并为一个表。请帮我解决这个问题,感谢您的帮助。

最佳答案

这是你需要的吗?它在单独的子查询中查找每个可用员工的批准和拒绝的休假计数,然后将其与员工表连接以在一行中显示每个员工的拒绝和批准的休假计数。

如果计数为空,则使用coalesce(col,0)返回0。合并返回给定参数列表中的第一个非空值。

select
t1.*,
coalesce(t2.approvedleave,0) approvedleave
coalesce(t3.rejectedleave,0) rejectedleave
from employee t1
left join (
select employee_ic, count(*) approvedleave
from approvedleave
group by employee_ic
) t2 on t1.ic_no = t2.employee_ic
left join (
select employee_ic, count(*) rejectedleave
from rejectedleave
group by employee_ic
) t3 on t1.ic_no = t3.employee_ic;

在这里阅读有关合并的内容:

关于mysql - 如何在 phpmyadmin 中加入表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41911265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com