日期:2014-05-18 浏览次数:20530 次
select * from tb where UserID=1 or IsShared='Y'
------解决方案--------------------
use Tempdb go --> --> if not object_id(N'Tempdb..#T') is null drop table #T Go Create table #T([Id] int,[UserID] int,[IsShared] nvarchar(1)) Insert #T select 1,1,N'N' union all select 2,1,N'Y' union all select 3,2,N'N' union all select 4,2,N'Y' union all select 5,3,N'N' union all select 6,3,N'Y' Go DECLARE @userID INT SET @userid=1 Select * from #T AS a WHERE UserID=@userid OR [IsShared]='Y' /* Id UserID IsShared 1 1 N 2 1 Y 4 2 Y 6 3 Y */
------解决方案--------------------
1:熟悉 AND OR 的用法
2:在相同关系 的地方 用括号括起来
select * from tb where UserID=1 or showid=2 or IsShared='Y'
select * from tb where UserID=1 AND showid=2 or IsShared='Y'
select * from tb where (UserID=1 AND showid=2) or IsShared='Y'
select * from tb where UserID=1 AND (showid=2 or IsShared='Y')
分别试试 再理解一下就可灵活使用