一条查询语句,思路混乱了
本帖最后由 SkyLee708 于 2013-08-27 21:03:06 编辑 比如 某表
A B C D
张三 10 0
张三 0 10
李四 10 0
王五 10 10
赵六 0 10
孙七 20 20
'select * from 表 where D <> 0 and A like''%张三%'' and B=10 or C=10'
Declare @table table(A nvarchar(10),B int ,C int ,D int)
insert @table
select N'张三', 10, 0,nullunion all
select N'张三', 0,10,nullunion all
select N'李四', 10, 0,nullunion all
select N'王五', 10,10,nullunion all
select N'赵六', 0,10,nullunion all
select N'孙七', 20,20,null
select *
from @table t
where t.A like '%张三%'
--and (t.B=10 or t.C=10)--B和C 判断 根据你的需要 是否要添加吧
/*
ABCD
张三100NULL
张三010NULL
*/
create table #a(A varchar(10),B int,C int,D int)
insert into #a(A,B,C)
select '张三',10,0
union all select '张三',0,10
union all select '李四',10,0
union all select '王五',10,10
union all select '赵六',0,10
union all select '孙七',20,20
select * from #a
where A='张三'
/*
ABCD
-------------------------
张三100NULL
张三010NULL
*/
if OBJECT_ID('tempdb..#temp', 'u') is not null drop table #temp;
go
create table #temp( [A] varchar(100), [B] varchar(100), [C] varchar(100), [D] varchar(100));
insert #temp
select '张三','10','0','' union all
select '张三','0','10','' union all
select '李四','10','0','' union all
select '王五','10','10','' union all
select '赵六','0','10','' union all
select '孙七','20','20',''
--SQL:
select * from #temp where D <> '0' and A LIKE '%张三%' and (B=10 or C=10)
/*
ABCD
张三100
张三010
*/