简体   繁体   English

如何在Datetime中使用NOT IN或Not Exist自定义?

[英]How to use NOT IN or Not Exist custom in Datetime?

i use 2 table, i want first table not data in table two of show on to database. 我使用2表,我希望第一个表不是显示在数据库的表二中的数据。

Ex. 防爆。 * First Table "filesTA" * * 第一表“ filesTA” *

EmpNo | ChkDate
00001 | 2012-10-01 00:00:00.000
00001 | 2012-10-02 00:00:00.000
00001 | 2012-10-03 00:00:00.000
00001 | 2012-10-04 00:00:00.000
00001 | 2012-10-05 00:00:00.000

"SalaryDay2" “ SalaryDay2”

sEmpNo | sDate
00001 | 2012-10-01 00:00:00.000
00001 | 2012-10-02 00:00:00.000

When i select datetime between 2012-10-01 and 2012-10-05 当我在2012-10-01和2012-10-05之间选择日期时间时

i need output: 我需要输出:

sEmpNo | sDate
00001 | 2012-10-03 00:00:00.000
00001 | 2012-10-04 00:00:00.000
00001 | 2012-10-05 00:00:00.000

this code: 此代码:

SELECT tf.EmpNo,tf.ChkDate
FROM filesTA tf 
WHERE tf.ChkDate NOT IN(
SELECT sd2.sDate
FROM SalaryDay2 sd2
WHERE Convert(nvarchar(10),sd2.sDate,126) Between '2012-10-01' and '2012-10-05'
)

please help me. 请帮我。 thanks you for you time.:) 谢谢您的时间。:)

Unless I misunderstood your question, you need something like this: 除非我误解了您的问题,否则您需要以下内容:

SELECT tf.EmpNo,tf.ChkDate
FROM filesTA tf 
LEFT JOIN SalaryDay2 sd2 ON (sd2.EmpNo = tf.EmpNo AND sd2.ChkDate = tf.ChkDate)
WHERE sd2.EmpNo IS NULL  --you may want to add other condition as well

(records from filesTA for which there is no corresponding records in SalaryDay2) (来自文件TA的记录,在SalaryDay2中没有相应的记录)

BETWEEN is an inclusive thing, basically boiling down to BETWEEN是一个包容性的事物,基本上可以归结为

x BETWEEN a AND b

being the same as 与...相同

(x >= a) AND (x <= b)

if I'm reading your sample correctly, you want a non-inclusive version, which you'll have to write out the hard way: 如果我正确地阅读了您的样本,则需要一个非包容性版本,您必须用困难的方式写出:

yourdate > '2012-10-01' AND yourdate <= '2012-10-05'
         ^-- note: <, not <=         

Use EXCEPT clause 使用EXCEPT子句

SELECT EmpNo, ChkDate
FROM filesTA
EXCEPT
SELECT sEmpNo, sDate
FROM SalaryDay2
WHERE sDate BETWEEN '20121001' and '20121005' 

Demo on SQLFiddle 关于SQLFiddle的演示

You can use NOT EXISTS for this purpose. 您可以为此使用NOT EXISTS It should eliminate lines that doesn't exist in your second table, based on your criteria for joining. 根据您的加入条件,它应该消除第二个表中不存在的行。

DECLARE @FirstDate DATE = '2012-10-01'
DECLARE @SecondDate DATE = '2012-10-05'
SELECT  *
FROM    FilesTA
WHERE   NOT EXISTS ( SELECT 1
                     FROM   SalaryDay2
                     WHERE  SalaryDay2.sDate = FilesTA.ChkDate 
                     -- If you want to check per EmpNo as well uncomment this line:
                     -- AND SalaryDay2.sEmpNo = FilesTA.EmpNo
)
        AND FilesTA.ChkDate BETWEEN @FirstDate
                            AND     @SecondDate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM