在SQL Server中组合来自不同表和列的两个查询

时间:2018-02-25 11:36:34

标签: sql-server tsql

我有两个SQL Server查询,我需要在C#gridview中显示最终输出结果:

Select *
From PayBack 
Where DATEDIFF(day, GetDate(), (Expirydate)) < 0 
  and DATEPART(yyyy, Expirydate) = '2018' 
  and DATEPART(Month,Expirydate) = '02' 

Select Nomclient, Numero1, Numero2, Numero3, Email1, Email2 
From Client 
Where Nomclient In (Select Client from PayBack)

如何将此合并为一个查询?

2 个答案:

答案 0 :(得分:1)

试试这个 -

Select PB.*,C.Nomclient,C.Numero1,C.Numero2,C.Numero3,C.Email1,C.Email2 
From PayBack as PB
Inner Join Client as C on C.Nomclient = PB.Client
Where DATEDIFF(day,GetDate(),(PB.Expirydate))<0 and
DATEPART(yyyy,PB.Expirydate) = '2018' and DATEPART(Month,PB.Expirydate)='02' 

答案 1 :(得分:0)

Select 
    Nomclient,Numero1,Numero2,Numero3,Email1,Email2 
from 
    Client 
where 
    Nomclient in (
        Select Client 
        From PayBack 
        where 
            DATEDIFF(day,GetDate(),(Expirydate))<0 and DATEPART(yyyy,Expirydate) = '2018' 
            and DATEPART(Month,Expirydate)='02'
    )