如何在同一个查询中使用like和= [英] How to use like and = in same query
本文介绍了如何在同一个查询中使用like和=的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在选择语句中同时使用like和=。
这里是查询select * from tblxyz其中名称如@ a1或mob喜欢@ a2和city = @ a3
使用这些类型查询的正确方法是什么。
请尽快回复。
How to use both "like" and "=" in select statment.
here is the query "select * from tblxyz where name like @a1 or mob like @a2 and city = @a3"
What is the right method to use these type of queries.
please reply soon.
推荐答案
你的问题不是使用'='和'like'而是使用'和'和'或'没有括号。尝试
Your problem is not with the use of '=' and 'like' but with the use of 'and' and 'or' without parentheses. Try
select * from tblxyz where name like @a1 or ( mob like @a2 and city = @a3 )
注意我已经猜到了你的实际标准
编辑 - 在这里回应OP查询是一个有用的教程,解释了查询中括号的使用 [ ^ ]
DECLARE @SampleTable AS TABLE(Sno INT IDENTITY(1,1),
Name NVARCHAR(20),
Mob NVARCHAR(20),
City NVARCHAR(20))
INSERT INTO @SampleTable (Name,Mob,City) VALUES ('Balaji','23123123','Trichy')
INSERT INTO @SampleTable (Name,Mob,City) VALUES ('Dani','453452345','Dalmia')
INSERT INTO @SampleTable (Name,Mob,City) VALUES ('Leo','4536423','Erode')
INSERT INTO @SampleTable (Name,Mob,City) VALUES ('Micheal','734525','Coimbatore')
INSERT INTO @SampleTable (Name,Mob,City) VALUES ('Nelson','762345245','Chennai')
DECLARE @NAME NVARCHAR(20) = 'Dani'
DECLARE @Mob NVARCHAR(20) = '453452345'
DECLARE @City NVARCHAR(20) = 'Dalmia'
-- Use Can Use Like this
SELECT Sno,Name,Mob,City FROM @SampleTable WHERE (Name LIKE @NAME OR Mob LIKE @Mob) AND City = @City
-- Best Way of Using Like Querry is with %
SET @NAME = 'Da'
SET @Mob = '234'
SELECT Sno,Name,Mob,City FROM @SampleTable WHERE (Name LIKE +'%' + @NAME +'%' OR Mob LIKE +'%' + @Mob +'%' ) AND City = @City
这篇关于如何在同一个查询中使用like和=的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文