在SQLserver中使用子查询获取LIKE子句的内容 [英] Using Subquery for the content for LIKE clause in SQLserver

查看:523
本文介绍了在SQLserver中使用子查询获取LIKE子句的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个查询

Hi,

I have a query

Select * from CityTable where CountryId="somevalue" and CityName LIKE '%andhra%'

.

取而代之的是,我想使用子查询的输出来喜欢claues

.

Instead of this i would like to use the output of a subquery to like claues

Select * from CityTable where CountryId="somevalue" and CityName LIKE '%
 here I want to use  a query(select cityname from Allcity where CityID='1')%'.



如何使用.



How can I use.

推荐答案


试试这个:
Hi,
Try this:
SELECT * FROM CityTable
WHERE CountryId="IND" 
AND CityName LIKE
'%'+(SELECT CityName FROM Allcity WHERE CityID='1')+'%'



--Amit



--Amit


您好,Velkumar Kannan,

如果您的子查询返回单个值,则可以使用此查询

Hi Velkumar Kannan,

If your subquery returns single value you can use this query

Select * from CityTable where CountryId="somevalue" and 
CityName like '%'+ (select cityname from Allcity where CityID='1')+ '%'


Select * from CityTable 
where CountryId="somevalue" 
and CityName LIKE '%' + (select max(cityname) from Allcity where CityID='1') + '%'


LIKE运算符接收一个表达式,不一定是硬编码的常量.

希望这会有所帮助,
巴勃罗.


The LIKE operator receives an expression, not necesarily a hard-coded constant.

Hope this helps,
Pablo.


这篇关于在SQLserver中使用子查询获取LIKE子句的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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