请教这样的一条语句怎么写

fancydong 2014-04-24 06:12:58
有两张表,两张表只有一个字段TESTNO,所有数据都是5位,现在需要从A表中筛选出数据,这些数据具有的特征是连续的4位数在B表中没有此特征的数据(即B表中的数据没有连续的4位数和此条数据有相同特征),举例A表中23431,使用2343和3431在b表中找有没有含有2343和3431的数据,没有的话则插入C表
最好给出的是循环遍历的语句,因为AB两张表是动态的,定时会更新
a表
TESTNO
23431
12346
66547
54515
b表
TESTNO
42346
66548
62498
52922


查询出来的C表结果
23431
54515
...全文
227 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fancydong 2014-04-28
  • 打赏
  • 举报
回复
select a.testno from #b b
right join #a a on charindex(substring(a.testno,1,4),b.testno)>0 or 
charindex(substring(a.testno,2,4),b.testno)>0
where b.testno is null
中的charindex(substring(a.testno,2,4),b.testno)>0应该是这个吧charindex(substring(a.testno,2,5),b.testno)>0
山寨DBA 2014-04-25
  • 打赏
  • 举报
回复
引用 3 楼 zssfrank 的回复:
[quote=引用 1 楼 hwhmh2010 的回复:] 这样不行吗:

select a.* from a where TESTNO not in(
	select b.TESTNO from a,b 
	where left(cast(a.TESTNO as varchar(5)),4)=left(cast(b.TESTNO as varchar(5)),4)
	)
干嘛要用循环遍历呢,你那表里会有数据插入的话,你循环的时候会锁表,插入不成功咋整嗫?或者说你循环的时候加了with nolock ,可以插入数据,但是会有脏读的哟
你写的太麻烦了,一句not exists 就可以[/quote] not in和not exists性能上没太大差异。一句 not exists 也可以:

select a.TESTNO from a 
	where left(cast(a.TESTNO as varchar(5)),4) not exists(select left(cast(b.TESTNO as varchar(5)),4) from b)
习惯性蹭分 2014-04-25
  • 打赏
  • 举报
回复

 select * from #b
create table #a(testno  char(5))
insert into #a
select '23431' union
select '12346' union
select '66547' union
select '54515'
 
create table #b(testno char(5))
insert into #b
select  '42346' union
select '66548' union
select '62498' union
select '52922'

select a.testno from #b b
right join #a a on charindex(substring(a.testno,1,4),b.testno)>0 or 
charindex(substring(a.testno,2,4),b.testno)>0
where b.testno is null
习惯性蹭分 2014-04-25
  • 打赏
  • 举报
回复

select a.testno from #b b
right join #a a on charindex(substring(a.testno,1,4),b.testno)>0 or 
charindex(substring(a.testno,2,4),b.testno)>0
where b.testno is null

u010553042 2014-04-25
  • 打赏
  • 举报
回复
看清楚条件,举例说明abcde是否有子字符串包含在edcba中,要取abcd比较edcb和dcba,还要取bcde比较edcb和dcba,上述各位的条件都不正确啊
在路上_- 2014-04-25
  • 打赏
  • 举报
回复
俺只赞2楼,其它的真对吗?
cy2889792 2014-04-25
  • 打赏
  • 举报
回复
So Easy 一条语句搞定,楼主给分

SELECT TESTNO FROM A
WHERE NOT EXISTS (
	SELECT TESTNO FROM B 
	WHERE LEFT(B.TESTNO,4)<>LEFT(A.TESTNO,4) AND RIGHT(B.TESTNO,4)<>RIGHT(A.TESTNO,4) 
)
zssfrank 2014-04-24
  • 打赏
  • 举报
回复
引用 1 楼 hwhmh2010 的回复:
这样不行吗:

select a.* from a where TESTNO not in(
	select b.TESTNO from a,b 
	where left(cast(a.TESTNO as varchar(5)),4)=left(cast(b.TESTNO as varchar(5)),4)
	)
干嘛要用循环遍历呢,你那表里会有数据插入的话,你循环的时候会锁表,插入不成功咋整嗫?或者说你循环的时候加了with nolock ,可以插入数据,但是会有脏读的哟
你写的太麻烦了,一句not exists 就可以
  • 打赏
  • 举报
回复
--> 测试数据: [a]
if object_id('[a]') is not null drop table [a]
create table [a] ([TESTNO] int)
insert into [a]
select 23431 union all
select 12346 union all
select 66547 union all
select 54515

--select * from [a]

--> 测试数据: [b]
if object_id('[b]') is not null drop table [b]
create table [b] ([TESTNO] int)
insert into [b]
select 42346 union all
select 66548 union all
select 62498 union all
select 52922

--select * from [b]

select *
from a
except
select a.* 
from b join  a on b.testno like '%'+LEFT(a.testno,4)+'%' or b.testno like '%'+right(a.testno,4)+'%'

23431
54515
山寨DBA 2014-04-24
  • 打赏
  • 举报
回复
这样不行吗:

select a.* from a where TESTNO not in(
	select b.TESTNO from a,b 
	where left(cast(a.TESTNO as varchar(5)),4)=left(cast(b.TESTNO as varchar(5)),4)
	)
干嘛要用循环遍历呢,你那表里会有数据插入的话,你循环的时候会锁表,插入不成功咋整嗫?或者说你循环的时候加了with nolock ,可以插入数据,但是会有脏读的哟

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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