像多列查询一样

时间:2015-08-17 08:50:57

标签: sql sql-server

在做这个查询时我得到了

转换nvarchar值时转换失败' Dentist'数据类型int。

如何解决这个问题?

select * from tblBusinessCategory as b
inner join tblUser as u
on b.BusinessID=u.BusinessCategoryId
inner join tblAddress as a
on u.AddressId=a.AddressID
where u.BusinessCategoryId in (select BusinessCategory from tblBusinessCategory where BusinessCategory LIKE '%d%') 

2 个答案:

答案 0 :(得分:3)

问题出在您的嵌套查询中:

in (select BusinessCategory from tblBusinessCategory where BusinessCategory LIKE '%d%')

应该是

in (select BusinessCategoryId from tblBusinessCategory where BusinessCategory LIKE '%d%')

答案 1 :(得分:2)

<强>问题:

您正在子查询中选择BusinessCategory,即varchar。尝试将BusinessCategory转换为int类型时失败。

<强>解决方案:

您应该从表格BusinessCategoryId中选择tblBusinessCategory

............
where u.BusinessCategoryId in (select BusinessCategoryId from tblBusinessCategory where BusinessCategory LIKE '%d%') 

修改

要对多个列执行类似操作,您可以执行以下操作:

where u.BusinessCategoryId in 
   (select BusinessCategoryId 
    from tblBusinessCategory 
    where BusinessCategory LIKE '%d%' 
       or BusinessName LIKE '%something%'
       or BusinessDescription like '%something%')