1

我使用 2 个表,我希望第一个表而不是表 2 中的数据显示到数据库中。

前任。 *第一个表“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

“工资日2”

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

当我在 2012-10-01 和 2012-10-05 之间选择日期时间时

我需要输出:

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

这段代码:

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'
)

请帮我。谢谢你的时间。:)

4

4 回答 4

1

除非我误解了你的问题,否则你需要这样的东西:

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

(来自 filesTA 的记录,在 SalaryDay2 中没有相应记录)

于 2012-12-10T17:25:02.530 回答
0

您可以NOT EXISTS用于此目的。它应该根据您的加入标准消除第二个表中不存在的行。

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
于 2012-12-11T09:50:56.007 回答
0

BETWEEN是一个包容的东西,基本上归结为

x BETWEEN a AND b

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

如果我正确阅读了您的示例,则您需要一个非包含版本,您必须以艰难的方式写出:

yourdate > '2012-10-01' AND yourdate <= '2012-10-05'
         ^-- note: <, not <=         
于 2012-12-10T17:24:41.797 回答
0

使用EXCEPT子句

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

SQLFiddle上的演示

于 2012-12-11T09:41:15.483 回答