首页 > 解决方案 > 如何使用不同的记录值两次查询表中的列?

问题描述

我在表名 ActionFlag 中有一个列,其中包含“N”、“R”和“A”等值,但是当我尝试查询此语句时,什么也没有出现?

select tblFtpTrans.*,tblLocs.unit from tblFtpTrans left join tblLocs on 
tblFtpTrans.location=tblLocs.location where tblFtpTrans.id>0 and actiondate>='2020-06-01' and 
actiondate<='2020-07-30' and actionflag='N' AND ActionFlag='A' 

样本数据

NumId   WorkId  EightId Num ActionFlag
24803   17944   7683    0   N
24804   17940   5076    0   A
24805   17896   7684    0   A
24806   17904   7685    0   B
24807   17868   7686    0   R

这就是我要的

NumId   WorkId  EightId Num ActionFlag
24803   17944   7683    0   N
24804   17940   5076    0   A
24805   17896   7684    0   A

标签: sqlsql-server

解决方案


只需使用in

select t.*, l.unit
from tblFtpTrans t left join
     tblLocs l
     on t.location = l.location
where t.id > 0 and
      actiondate >= '2020-06-01' and 
      actiondate <= '2020-07-30' and
      actionflag in ('N', 'A')

推荐阅读