douzhi8244 2015-10-08 05:15
浏览 136
已采纳

工资表的MY SQL查询

I have 3 tables in MYSQL From 2 different database running in same server

enter image description here

and my expected Output is

enter image description here

MY attempt of query is

SELECT `biometric`.`Empid`,
,`name`,`location`,`basic`,`hra`,`conveyance`,`total salary`,sum(DISTINCT DATEDIFF(LEAST(`endDate`,'$monthEnd' ),GREATEST(`startdate`,'$monthStart'))+1) as leaves FROM `biometric`.`biometric`,`biometric`.`employee` JOIN `lms`.`leaves` WHERE `biometric`.`empid`= `employee`.`empid` AND `employee`.`empid` = `leaves`.`id` AND  
`startdate`<='$monthEnd' AND `endDate`>= '$monthStart' AND `leaves`.`status` = '3'
GROUP BY `Empid`

and here status =3 is approved leave and 1 is non approved leave. and my out put coming is

enter image description here Thanks in Advance...

  • 写回答

1条回答 默认 最新

  • dongyan1993 2015-10-08 05:51
    关注

    You can try below query, even I did not test it so if you get any error then you can create an sqlfiddle so that I can correct it.

    Basic concept is that you need to use left join for lms.leaves table as you need all employee details even they did not took leave under status 3.

    SELECT bmt.`Empid`,`name`,`location`,`basic`,`hra`,`conveyance`,`total salary`,
    IFNULL(SUM(DISTINCT DATEDIFF(LEAST(`endDate`,'$monthEnd' ),GREATEST(`startdate`,'$monthStart'))+1),0) AS LEAVES 
    FROM BM.`biometric` AS bmt
    JOIN BM.`employee` AS emp ON bmt.`empid`= emp.`empid`
    LEFT JOIN `lms`.`leaves` AS lvs ON emp.`empid` = lvs.`id` AND lvs.`status` = '3' AND `startdate`<='$monthEnd' AND `endDate`>= '$monthStart' 
    GROUP BY bmt.`Empid`;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题