现在有三个表 a b c
表a PayCalculation 里面有(paydate empid 等等)
表b Employee有(empid dateleft 等)
表c 里面(都跟表a一样)现已经表a跟表b合并起来
SELECT PayCalculation.*, Employee.DateLeft FROM PayCalculation INNER JOIN Employee ON Employee.EmployeeID = PayCalculation.EmpID现在又想把表c里面的paydate 一起查询出来 
但是paydate年月跟表a一样的不显示出来 
请问现在该怎么写

解决方案 »

  1.   

    SELECT 
      PayCalculation.*, 
      Employee.DateLeft 
    FROM 
      (select * PayCalculation 
       union all
       select * from c where not exists(select 1 from PayCalculation where empid=c.empid and datediff(mm,paydate,c.paydate)=0)
      ) as PayCalculation
    INNER JOIN 
      Employee 
    ON 
      Employee.EmployeeID = PayCalculation.EmpID
      

  2.   

    SELECT 
      PayCalculation.*, 
      Employee.DateLeft 
    FROM 
      (select * from PayCalculation 
       union all
       select * from c where not exists(select 1 from PayCalculation where empid=c.empid and datediff(mm,paydate,c.paydate)=0)
      ) as PayCalculation
    INNER JOIN 
      Employee 
    ON 
      Employee.EmployeeID = PayCalculation.EmpID树哥少了个from哦! 
      

  3.   

    select
      a.*,b.DateLeft,c.*
    from
      PayCalculation a
    inner join
      Employee b
    ON 
      b.EmployeeID = a.EmpID 
    inner join
      (select * from c where not exists(select 1 from PayCalculation where year(paydate)=year(c.paydate) and month(paydate)=month(c.paydate))
    on
      b.EmployeeID =c.EmpID
      

  4.   

    SELECT 
      a.*, 
      b.DateLeft 
    FROM 
      (select * from PayCalculation 
       union all
       select * from c where not exists(select 1 from PayCalculation where empid=c.empid and year(paydate)=year(c.paydate) and month(paydate)=month(c.paydate))
      ) a
    INNER JOIN 
      Employee b
    ON 
      b.EmployeeID = a.EmpID