SQL如何在两个表中插入NOT EXISTS Criteria

时间:2016-10-25 10:48:12

标签: sql where exists

我有两张桌子T1和amp; T2

T1 has ID, Sl_No and Active (1, 123, 'Y')
T2 has ID, Sl_No, tDate and OutYesOrNo (1, 123, tdate(CURDATE()), 'Y')

T1是主表。我想将T1中的Sl_No插入T2,其中T1.Active ='Y'和T2.OutYesorNo<> 'Y'和T2.tDate不存在。

我尝试了这个,但它无法正常工作

INSERT INTO T2 (Sl_No)
SELECT Sl_No FROM T1
WHERE (T1. Active = 'Y' OR T1.Active = 'y') 
AND NOT EXISTS (SELECT Sl_No From T2 WHERE tDate = CURDATE()
                AND T2.OutYesorNo='Y' OR T1.OutYesorNo = 'y')

0 个答案:

没有答案