2
Delete From StudentTb Where StudentType in (1, 2) and StudentI not in
(
    Select lg.StudentI From StudentLog lg
)

我试图将上面的代码更改为以下...但是,我不确定它是否正确。

Delete From StudentTb Where StudentType in (1, 2) 
    and Not Exists 
    (
        Select 'x' 
        From StudentLog lg                          
        Where StudentI= lg.StudentI
    )

谁能帮帮我吗?

4

3 回答 3

2
Delete From StudentTb  s
Where StudentType in (1, 2) 
and Not Exists 
(
    Select *
    From StudentLog                          
    Where StudentI = s.StudentI
)

在子查询中,对列的非限定引用(不指定表名或别名)首先被假定在子查询本身引用的表中,而不是在外部查询的其他表中。因此,在您的 sql 语法中,谓词的两侧是子查询表Where StudentI = lg.StudentI中的同一列StudentLog

于 2013-11-06T17:28:11.700 回答
2

您做错的只是没有在子查询中指定正确的别名:

Delete From StudentTb Where StudentType in (1, 2) 
    and Not Exists 
    (
        Select 'x' 
        From StudentLog lg                          
        Where StudentTb.StudentI= lg.StudentI
    )

注意 - select 语句中的列不会影响最终结果,但仍然首选 write*而不是'x'

于 2013-11-06T17:31:33.527 回答
1

not in 和 not exist 并不总是具有相同的含义。我假设您想要转换,因为“不在”往往很慢。这是逻辑始终匹配的另一种方式。

Delete From StudentTb 
Where StudentType in (1, 2) 
and StudentI in
(
select StudentI
from StudentTb
except 
Select StudentI From StudentLog    
)
于 2013-11-06T17:32:10.580 回答