这个SQL语句如何写

yuyu2002 2003-12-16 10:11:12
我希望将记录显现出来,当A表的“地址”列于B表的“地址”列有连续的5个字符相同时(甚至更多)
这个SQL语句怎么写
...全文
20 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuyu2002 2003-12-16
  • 打赏
  • 举报
回复
对不起大家,没讲清除
曾做了2个用户表,现在我想将A用户表和B用户表统一起来,
剔除相同的用户,是在SQLServer2000中,用手动的方式

判断用户的标志是“地址”列
但由于当初输入的随意性,比如相同的一个用户
在A表的地址列“浦东:田园小区148号405室”
在B表的地址列“浦:田园小区148幢405室”

故我希望通过查询语句能将两表中“地址列”
连续5个字符(也可能更多)相同的记录列出,
再看看,手动删去
yuyu2002 2003-12-16
  • 打赏
  • 举报
回复
我的意思是:
我想将A用户表和B用户表统一起来,判断用户的标志是“地址”列
但由于当初输入的随意性,故我希望能将有连续5个字符相同的记录列出,
再看
wzh1215 2003-12-16
  • 打赏
  • 举报
回复
太难了!
txlicenhe 2003-12-16
  • 打赏
  • 举报
回复
...where charindex(left(a.地址,5),b.地址)>0

???

zjcxc 元老 2003-12-16
  • 打赏
  • 举报
回复
难.
victorycyz 2003-12-16
  • 打赏
  • 举报
回复
这个比较难,我可能写不出来。

不过,想问一下,你这样的条件是不是不太合理?试想,假设A表中有10条记录带有" road"字串,B表中有20条记录带有此字串。那你怎么选取?选出10*20条记录吗?有意义吗?
smallroad 2003-12-16
  • 打赏
  • 举报
回复
用substring试试
yuyu2002 2003-12-16
  • 打赏
  • 举报
回复
高手好多啊,谢谢大家,等我试验好了散分
gmlxf 2003-12-16
  • 打赏
  • 举报
回复
/*
函数名:f_CompTwoStr
功能:比较两个字符串是否有连续相同个数的字符
参数:
@str1---字符串1
@str1---字符串2
@n---连续相同的个数
返回:
1---存在
0---不存在
*/

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_CompTwoStr]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_CompTwoStr]
go

create function f_CompTwoStr
(
@str1 varchar(8000),
@str2 varchar(8000),
@n int
) returns bit
as
begin
declare @i int
set @i=1
while @i<=len(@str1)-@n+1
begin
if charindex(substring(@str1,@i,@n),@str2)>0
return 1
set @i=@i+1
end
return 0
end
go

--test:
select dbo.f_CompTwoStr('浦东:田园小区148号405室','浦:田园小区148幢405室',5)
select dbo.f_CompTwoStr('浦东:田园小区148号405室','浦:田园小区148幢405室',8)
select dbo.f_CompTwoStr('浦东:田园小区148号405室','浦:田园小区148幢405室',9)
zjcxc 元老 2003-12-16
  • 打赏
  • 举报
回复
--比较第一与第二个字符串,是否有连续的5个字符相同,如果有,返回1,否则返回0

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_compstr]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_compstr]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[序数表]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [序数表]
GO

--为提高效率,最好创建一个固定的序数表
select top 1600 id=identity(int,1,1) into 序数表 from sysobjects a,sysobjects b

go
create function f_compstr(
@str1 varchar(8000),
@str2 varchar(8000)
) returns bit
as
begin
declare @re bit
/*--如果使用动态序数,效率极低.
declare @tb table(id int identity(1,1),a int)
insert into @tb(a) select top 1600 null from sysobjects a,sysobjects b
--*/

if len(@str1)>len(@str2) set @re=case when exists(
select 1
from (select name=@str2)a,序数表 b --使用动态序数,改为: ,@tb b
where b.id<=len(@str2)-4 and @str1 like '%'+substring(name,b.id,5)+'%'
) then 1 else 0 end
else set @re=case when exists(
select 1
from (select name=@str1)a,序数表 b --使用动态序数,改为: ,@tb b
where b.id<=len(@str1)-4 and @str2 like '%'+substring(name,b.id,5)+'%'
) then 1 else 0 end
return(@re)
end
go

--调用示例
select dbo.f_compstr('浦东:田园小区148号405室','浦:田园小区148幢405室')
go
zjcxc 元老 2003-12-16
  • 打赏
  • 举报
回复
--比较第一与第二个字符串,是否有连续的5个字符相同,如果有,返回1,否则返回0

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_compstr]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_compstr]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[序数表]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [序数表]
GO

--为提高效率,最好创建一个固定的序数表
select top 1600 id=identity(int,1,1) into 序数表 from sysobjects a,sysobjects b

go
create function f_compstr(
@str1 varchar(8000),
@str2 varchar(8000)
) returns bit
as
begin
declare @re bit
/*--如果使用动态序数,效率极低.
declare @tb table(id int identity(1,1),a int)
insert into @tb(a) select top 1600 null from sysobjects a,sysobjects b
--*/

if len(@str1)>len(@str2) set @re=case when exists(
select 1
from (select name=@str2)a,序数表 b --使用动态序数,改为: ,@tb b
where b.id<=len(@str2)-4 and @str1 like '%'+substring(name,b.id,5)+'%'
) then 1 else 0 end
else set @re=case when exists(
select 1
from (select name=@str1)a,序数表 b --使用动态序数,改为: ,@tb b
where b.id<=len(@str1)-4 and @str2 like '%'+substring(name,b.id,5)+'%'
) then 1 else 0 end
return(@re)
end
go

--调用示例
select dbo.f_compstr('浦东:田园小区148号405室','浦:田园小区148幢405室')
go
zjcxc 元老 2003-12-16
  • 打赏
  • 举报
回复
--使用例子,查询不重复的记录,重复的,只显示第一条:
declare @t table(id int identity(1,1),address varchar(1000))
insert into @t
select '浦东:田园小区148号405室'
union all select '浦:田园小区148幢405室'
union all select '浦:田园小区148幢405室'
union all select '浦:田园小区148幢405室'
union all select 'abcdefghijklmn'
union all select 'abcdefg'
union all select 'abcdefghijklmn'
union all select 'abcdefg'
union all select '123456768898'
union all select '123345'

select * from @t a
where id=(select min(id) from @t where dbo.f_compstr(a.address,address)=1)

/*--测试结果

id address
----------- ----------------------------
1 浦东:田园小区148号405室
5 abcdefghijklmn
9 123456768898
10 123345
--*/
hivak47 2003-12-16
  • 打赏
  • 举报
回复
楼上的也许能行吧,但你的问题有的难呀,看看帮助吧。。。。学习学习!!!!!!!!!!!!!
zjcxc 元老 2003-12-16
  • 打赏
  • 举报
回复
--比较第一与第二个字符串,是否有连续的5个字符相同,如果有,返回1,否则返回0

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_compstr]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_compstr]
GO

create function f_compstr(
@str1 varchar(8000),
@str2 varchar(8000)
) returns bit
as
begin
declare @tb table(id int identity(1,1),a int)
declare @re bit

insert into @tb(a) select top 1600 null from sysobjects a,sysobjects b

if len(@str1)>len(@str2) set @re=case when exists(
select 1
from (select name=@str2)a, @tb b
where b.id<=len(@str2)-4 and @str1 like '%'+substring(name,b.id,5)+'%'
) then 1 else 0 end
else set @re=case when exists(
select 1
from (select name=@str1)a, @tb b
where b.id<=len(@str1)-4 and @str2 like '%'+substring(name,b.id,5)+'%'
) then 1 else 0 end
return(@re)
end
go

--调用示例
select dbo.f_compstr('浦东:田园小区148号405室','浦:田园小区148幢405室')
azsoft 2003-12-16
  • 打赏
  • 举报
回复
delete from a where 地址 in (select * from b)

34,590

社区成员

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

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