像多列查询一样 [英] Like query with multiple columns

查看:119
本文介绍了像多列查询一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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%')













执行此查询时我得到了



#1)将nvarchar值'Dentist'转换为数据类型int时转换失败。如何解决这个问题??

#2)如何对多列执行类似操作?

#3)这可以在mvc4控制器中调用此查询吗?







While doing this query i got

#1 ) Conversion failed when converting the nvarchar value 'Dentist' to data type int. how to solve this ??
#2 ) How to perform like operation on multiple columns??
#3 ) Is this possible to call this query in mvc4 controller ??

推荐答案

如果您可以直接使用条件,为什么要使用子查询来过滤数据?!?



Why to use subquery to filter data, if you can use a "condition" directly?!?

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 b.BusinessCategory LIKE '%d%'





请看一下这篇优秀文章: SQL连接的可视化表示 [ ^ ]了解join的工作方式。



#1)将nvarchar值'Dentist'转换为数据类型int时转换失败。如何解决这个问题??

BusinessCategoryId BusinessCategory 。为了能够获得正确的数据,你必须写:



Please, take a look at this excellent article: Visual Representation of SQL Joins[^] to understand how join's work.

#1 ) Conversion failed when converting the nvarchar value 'Dentist' to data type int. how to solve this ??
BusinessCategoryId is not the same as BusinessCategory. To be able to get proper data, you have to write:

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



但是!由于性能问题,我不建议使用这样的子查询!



#2)如何对多列执行类似操作?

写出正确的WHERE语句!


But! I do not recommend to use subquery like this, because of performance issue!

#2 ) How to perform like operation on multiple columns??
Write proper WHERE statement!

WHERE ((Column1 Like '%a' AND (Column2 = 1 OR Column3 = 2)





#3)这可以在mvc4控制器中调用此查询吗?

我不明白你...;(



#3 ) Is this possible to call this query in mvc4 controller ??
I do not understand you... ;(


这篇关于像多列查询一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆