首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

一条查询话语,思路混乱了

2013-09-05 
一条查询语句,思路混乱了本帖最后由 SkyLee708 于 2013-08-27 21:03:06 编辑比如 某表ABCD张三100张三010

一条查询语句,思路混乱了
本帖最后由 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'

结果出来
A        B      C      D
张三     10     0
张三      0     10
王五     10     10
赵六      0     10
想要结果是
A        B      C      D
张三     10      0
张三      0     10

一条查询语句,思路混乱了,求老师指点思路 查询
[解决办法]
select * from 表 where D <> 0  and A like''%张三%'' and (B=10 or C=10)

[解决办法]
引用:
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

*/

[解决办法]
--你的D列没给出。
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
*/

热点排行