0

我有三个表typemaster,salaryaddition,salarydeduction

打字员

类型标识 | 类型名 |

1 |Act.Alw |

2 |食堂|

3 |Sht.Alw |

4 |LOP |

加薪

slno |员工|薪资类型| 金额 |

1 |1 |1 |200 |

工资扣除

员工 | 工资类型 | 金额 |

1 |2 |500 |

1 | 4 | 300 |

我要显示

员工 | 加法 | 金额 | 扣减 | 数量

1 | Act.Alw |200 | 食堂 | 500 |

1 | 空 | 空 | 洛普 | 300 |

我写查询,但它显示重复的结果

select a.employee,a.typename,a.amount,b.typename,b.amount from
(select employee,typename,amount from salaryaddition 
 join typemaster on     typeid=salaryaddition.salarytype) a,
(select employee,typename,amount from salarydeduction 
 join typemaster on typeid=salarydeduction.salarytype) b
where a.employee=b.employee
4

1 回答 1

0

鉴于您有一张员工表,您可以尝试类似

SELECT  e.employee,
        sat.typename,
        SUM(sa.amount) amount,
        sdt.typename,
        SUM(sd.amount) amount
FROM    employeemaster e LEFT JOIN
        salaryaddition sa   ON  e.employee = sa.employee LEFT JOIN
        typemaster sat  ON  sa.salarytype = sat.typeid LEFT JOIN
        salarydeduction sd  ON  e.employee = sd.employee LEFT JOIN
        typemaster sdt  ON  sd.salarytype = sdt.typeid
GROUP BY    e.employee,
            sat.typename,
            sdt.typename
于 2012-08-10T05:55:15.643 回答