表employee字段e_id主键e_name表vacation字段vacation_days,vacation_type,e_id外键表overtime字段pour_days,e_id外键以下为查询语句语句1SELECTemployee.e_name,employee.e_entrydat...
表 employee 字段 e_id 主键 e_name
表 vacation 字段 vacation_days,vacation_type,e_id外键
表 overtime 字段 pour_days, e_id外键
以下为查询语句
语句1
SELECT employee.e_name,employee.e_entrydate,
SUM(IF(vacation.vacation_type='1',vacation_days,0)) AS NX ,
SUM(IF(vacation.vacation_type='2',vacation_days,0)) AS DX
FROM employee LEFT JOIN vacation
ON employee.e_id=vacation.e_id
GROUP BY employee.e_id
返回结果
e_name e_entrydate NX DX
小红 1998-10-12 64 10
小赵 1988-07-05 10 0
小王 1995-05-02 10 0
这个查询没有问题
语句2
SELECT employee.e_name,employee.e_entrydate,
SUM(overtime.pour_days) AS POUR
FROM employee
LEFT JOIN overtime
ON employee.e_id=overtime.e_id
GROUP BY employee.e_id
返回结果
e_name e_entrydate POUR
小红 1998-10-12 6
小赵 1988-07-05 2
小王 1995-05-02 2
这个查询没有问题
问题出现3个表联合查询上面,语句如下:
SELECT employee.e_name,employee.e_entrydate,
SUM(IF(vacation.vacation_type='1',vacation_days,0)) AS NX ,
SUM(IF(vacation.vacation_type='2',vacation_days,0)) AS DX ,
SUM(overtime.pour_days) AS POUR
FROM employee LEFT JOIN vacation
ON employee.e_id=vacation.e_id
LEFT JOIN overtime
ON employee.e_id=overtime.e_id
GROUP BY employee.e_id
e_name,e_entrydate,NX,DX,POUR
小红 1998-10-12 192 30 42
小赵 1988-07-05 10 0 2
小王 1995-05-02 10 0 2
第三个查询的第一行数值和以前不一样了,不知道问题出在哪,单表查询,两个表联合查询都没有问题,只是如果3个表一起的话,这个数值就不对,请教高手!
展开