在select子句中获取的值中应用小于条件的删除查询

时间:2012-12-07 06:39:32

标签: sql-server

我想比较两个表T1和T2,并删除T1中的记录,其中form_no小于form_no选择形式T2。 我写过以下查询,但它无效。

DELETE FROM T1
WHERE --Some Condition--
AND
FORM_NO < (ISNULL((SELECT DISTINCT(FORM_NO) FROM T2 WHERE --Some Condition--), 0))

2 个答案:

答案 0 :(得分:1)

试试这个...希望你能为我们提供完整的查询..

DELETE FROM T1
WHERE --Some Condition--
AND FORM_NO IN (SELECT DISTINCT(t.FORM_NO) FROM T1 AS t
        LEFT JOIN T2 AS s
        ON -- COMMON COLUMN
        WHERE t.FORM_NO < s.FORM_NO)

答案 1 :(得分:0)

如果您有一个公共字段并想要删除此字段的加入:

delete from t11 
    from t1 t11
        join t2 on (t11.commonID=t2.commonID)
    where (t11.FORM_NO<t2.FORM_NO)

如果你想要的话就是这样:

delete from t1 where FORM_NO<(select min(FORM_NO) from t2)