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

SkyLee708 2013-08-27 09:01:39
比如 某表
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

一条查询语句,思路混乱了,求老师指点思路
...全文
149 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
SkyLee708 2013-08-28
  • 打赏
  • 举报
回复
引用 7 楼 iamggggs 的回复:
问题都说不清是够乱,D列还没数据…… 按我对你问题的理解,#1已经得分了。
水平有限,表达差,思维乱,感谢1#,2#,3# 二楼的老师带参数,学习ing~~~~~~
iamggggs 2013-08-27
  • 打赏
  • 举报
回复
问题都说不清是够乱,D列还没数据…… 按我对你问题的理解,#1已经得分了。
Shawn 2013-08-27
  • 打赏
  • 举报
回复
--你的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)
/*
A	B	C	D
张三	10	0	
张三	0	10	
*/
Andy__Huang 2013-08-27
  • 打赏
  • 举报
回复
想要这个结果很容易,是不是还有其他条件?
Andy__Huang 2013-08-27
  • 打赏
  • 举报
回复
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='张三'

/*
A	B	C	D
-------------------------
张三	10	0	NULL
张三	0	10	NULL

*/
偶麦嘎 2013-08-27
  • 打赏
  • 举报
回复
引用 1 楼 wmh178 的回复:
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,null	union all
select N'张三',  0,10,null	union all
select N'李四', 10, 0,null	union all
select N'王五', 10,10,null	union all
select N'赵六',  0,10,null	union all
select N'孙七', 20,20,null


select * 
from @table t
where t.A like '%张三%'
	--and (t.B=10 or t.C=10)--B和C 判断 根据你的需要 是否要添加吧
	
/*
	A	B	C	D
张三	10	0	NULL
张三	0	10	NULL
*/




Andy__Huang 2013-08-27
  • 打赏
  • 举报
回复
select * from tb where A='张三'
偶麦嘎 2013-08-27
  • 打赏
  • 举报
回复
select * from 表 where D <> 0 and A like''%张三%'' and (B=10 or C=10)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧