select T1.StateId, T1PointId
from Table1 T1
join Table2 FPMDE on T1.PointId = T2.PointID
where T1StateId = '1'
and T1StateId in (select convert (int, T3.StateName)
from Table3 T3
where T1.StateId = T3.StateId and T3.StateName = 'Alabama')
我收到上述查询的错误。能告诉我如何将varchar值转换为数据类型int吗?
答案 0 :(得分:1)
我认为您的预期查询是:
select T1.StateId, T1PointId
from Table1 T1 join
Table2 FPMDE
on T1.PointId = T2.PointID
where T1StateId = 1 and -- guessing that T1StateId is a number
exists (select 1
from Table3 T3
where T3.StateId = T1StateId and
StateName = 'Alabama'
) ;
注意:
in
和关联子句。答案 1 :(得分:0)
非常确定这是问题
cast (T1StateName as int)
仅基于列名称,我怀疑T1StateName有一些非整数
尝试
select distinct T1StateName
from - correctTableName
我假设T1StateId应该是T1.StateId
select T1.StateId, T1PointId
from Table1 T1
join Table2 FPMDE
on T1.PointId = T2.PointID
join Table3 T3
on T3.StateId = T1.StateId
and T3.StateName = 'Alabama'
where T1.StateId = 1
但这没有任何意义,因为除非T1.StateId = 1是'Alabama',否则你没有行
如果T1.StateId = 1是'Alabama'那么你根本不需要那部分
即使T1StateId是一个单独的列,它仍然只返回一个值,如果那是Alabama