sql在几分钟内提取数据未获得正确结果
我有这样一个事务表:sql在几分钟内提取数据未获得正确结果,sql,sql-server,Sql,Sql Server,我有这样一个事务表: Tbarcode PlateNo DelDate status 10 101 2014-03-26 12:14:10.000 4 11 102 2014-03-26 12:10:10.000 4 12 103 2014-03-26 12:05:10.000 5 13
Tbarcode PlateNo DelDate status
10 101 2014-03-26 12:14:10.000 4
11 102 2014-03-26 12:10:10.000 4
12 103 2014-03-26 12:05:10.000 5
13 104 2014-03-26 12:15:10.000 5
select t.TBarcode,t.PlateNo from transaction_tbl t where status in(4,5) and DATEDIFF(n,CAST(DelDate as datetime),GETDATE())<=3
我想获取状态4和状态5的所有记录,但状态5只需要DelDate+当前时间的3分钟。如果当前时间超过DelDate的3分钟,我不想显示状态5记录。我写了如下内容:
Tbarcode PlateNo DelDate status
10 101 2014-03-26 12:14:10.000 4
11 102 2014-03-26 12:10:10.000 4
12 103 2014-03-26 12:05:10.000 5
13 104 2014-03-26 12:15:10.000 5
select t.TBarcode,t.PlateNo from transaction_tbl t where status in(4,5) and DATEDIFF(n,CAST(DelDate as datetime),GETDATE())<=3
但这不起作用..我怎么能做到?任何帮助都非常有用试试这个
SELECT t.TBarcode ,
t.PlateNo
FROM transaction_tbl t
WHERE status = 4
UNION
SELECT t.TBarcode ,
t.PlateNo
FROM transaction_tbl t
WHERE status = 5
AND DATEDIFF(n, CAST(DelDate AS DATETIME), GETDATE()) <= 3
听起来你只需要用OR而不是IN来处理WHERE 比如:
SELECT t.TBarcode ,
t.PlateNo
FROM transaction_tbl t
WHERE [status] = 4
OR ([status] = 5 AND DATEDIFF(n, CAST(DelDate AS DATETIME), GETDATE()) <= 3)
怎么不工作?错误消息?错误的结果?得到错误的结果