Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql在几分钟内提取数据未获得正确结果_Sql_Sql Server - Fatal编程技术网

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)

怎么不工作?错误消息?错误的结果?得到错误的结果