【问题标题】:SQL Server 2012 query dateSQL Server 2012 查询日期
【发布时间】:2016-08-13 18:12:14
【问题描述】:

我怎样才能在 where 子句中为这样的事情返回值:

获取table1.de1,table2.de2,table3.de3,table4.de3中存在的所有记录

select * 
from table1
inner join table2
on table2.carID = table1.carID
inner join table3
on table3.carID = table1.carID
inner join table4
on table4.driverID = table1.driverID

recietrecord 存在于 table2 中,其 paydate 已过去 20 天,将其与 TODAYS 日期进行比较,并在名为 Days Passed From The Day Driver Was Suppose To Pay 的字段中显示这些日子

【问题讨论】:

  • 您可以在选择子句中使用DateDiff(dd, paydate, getdate()) 来显示日期差异。在 where 子句中,你可以有一个 where DateDiff(dd, paydate, getdate()) < 0 或适当的东西

标签: sql-server join


【解决方案1】:

首先,请使用table aliases。此解决方案适用于 SQL Server

正如在 cmets 中所写,您可以使用 DATEDIFF 函数将 paydateGETDATE 进行比较。

select  *,
        DATEDIFF(day,t2.paydate,GETDATE()) as [Days Passed From The Day Driver Was Suppose To Pay]
from table1 t1
inner join table2 t2
    on t2.carID = t1.carID
inner join table3 t3
    on t3.carID = t1.carID
inner join table4 t4
    on t4.driverID = t1.driverID
WHERE DATEDIFF(day,t2.paydate,GETDATE()) > 20

或者更好地使用分钟:

DATEDIFF(minute,t2.paydate,GETDATE()) > 28800 --60 minutes * 24 hours * 20 days 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 2019-12-06
    • 2018-02-14
    • 2010-11-13
    • 1970-01-01
    相关资源
    最近更新 更多