求 一个SQL 语句 在线等 谢谢

Owl_xiang 2008-09-11 11:28:03
有表User
字段:UserID,LoginName,PassWord
其中LoginName有如下示例数据:

haha50005
haha50006
の删除记忆…
の删除记忆”

现在需要有这样的SQL语句,能够将名字里面包含了两个以上相同文字和连续3个以上相同数字和字母的排除,只留一个

结果如:

Userid LoginName Password

1 haha50005 123
3 の删除记忆… 123
...全文
144 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangsp 2008-09-11
  • 打赏
  • 举报
回复
再帮顶一个
活跃一下人气
fangsp 2008-09-11
  • 打赏
  • 举报
回复
select * from [user] where id in (select min(id) from [user] group by left(LoginName,3))

试一下这个
-晴天 2008-09-11
  • 打赏
  • 举报
回复
能够将名字里面包含了两个以上相同文字和连续3个以上相同数字和字母的排除,只留一个

我理解楼主的要求是将如 abc1,abc2,abc3这样的名字归并,如果要将名字里任意字母组合的进行对比,那可不是小事情,至少得建一个大的词典,然后用词典进行逐个比较,烦着呐!
-晴天 2008-09-11
  • 打赏
  • 举报
回复

select * from [user] where id in (select min(id) from [user] group by left(LoginName,3))
dawugui 2008-09-11
  • 打赏
  • 举报
回复
直接SQL语句怕是难搞.使用存储过程来做,在里面用循环来做吧.
lgxyz 2008-09-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wufeng4552 的回复:]
declare @t table(ID int identity(1,1),loginname varchar(20),password varchar(10))
insert @t select 'haha50005','123'
insert @t select 'haha50006' ,465679
insert @t select 'の删除记忆…' ,456
insert @t select 'の删除记忆”',123456
select * from @t t where not exists(select 1 from @t where left(loginname,3)=left(t.loginname,3) and t.id>id)
這樣可以嗎?
[/Quote]

能够将名字里面包含了两个以上相同文字和连续3个以上相同数字和字母的排除,只留一个
这个你那句是不行的吧
Owl_xiang 2008-09-11
  • 打赏
  • 举报
回复
declare @t table(ID int identity(1,1),loginname varchar(20),password varchar(10))
insert @t select 'haha50005','123'
insert @t select 'haha50006' ,465679
insert @t select 'の删除记忆…' ,456
insert @t select 'の删除记忆”',123456
select * from @t t where not exists(select 1 from @t where left(loginname,3)=left(t.loginname,3) and t.id>id)
這樣可以嗎?
-----------------------

如果加这样一条数据就不能达到要求了。不过也已经很好了,谢谢
insert @t select 'h1ah1a50006' ,465679

你的这个语句只能筛选前面相同的
mpf1985 2008-09-11
  • 打赏
  • 举报
回复
strsql = "insert into Mmen SELECT Moff.office_name,Mtemp.signal_name,Mtemp.alm_time,Mtemp.clear_time " & _
"from Mtemp , Moff " & _
"where Mtemp.office_ID=Moff.office_ID and Mtemp.signal_name='门禁' " & _
"order by office_name,signal_name, xuhao"
这是一个写入的语句,供你参考
水族杰纶 2008-09-11
  • 打赏
  • 举报
回复
declare @t table(ID int identity(1,1),loginname varchar(20),password varchar(10))
insert @t select 'haha50005','123'
insert @t select 'haha50006' ,465679
insert @t select 'の删除记忆…' ,456
insert @t select 'の删除记忆”',123456
select * from @t t where not exists(select 1 from @t where left(loginname,3)=left(t.loginname,3) and t.id>id)
這樣可以嗎?
Owl_xiang 2008-09-11
  • 打赏
  • 举报
回复
所以才来请教大家
水族杰纶 2008-09-11
  • 打赏
  • 举报
回复
Select * from User a where not exists(select 1 from User where lefe(LoginName,3)=left(a.LoginName,3) and Userid>a.Userid)
dawugui 2008-09-11
  • 打赏
  • 举报
回复
这个有难度.
Owl_xiang 2008-09-11
  • 打赏
  • 举报
回复
多谢大家
lgxyz 2008-09-11
  • 打赏
  • 举报
回复
--函数
create function f_compstr(
@str1 varchar(8000),
@str2 varchar(8000)
) returns bit
as
begin
declare @re bit,@lstr varchar(8000),@sstr varchar(8000)
declare @tb table(id int identity(1,1),a int)
insert into @tb(a) select top 100 null from sysobjects a,sysobjects b

if len(@str1)>len(@str2)
select @lstr=@str1,@sstr=@str2
else select @lstr=@str2,@sstr=@str1

set @re=case when exists(
select 1
from (select name=@sstr)a,@tb b
where (b.id<=len(@sstr)-2 and @lstr like '%'+substring(name,b.id,3)+'%')
or (b.id<=len(@sstr)-1 and @lstr like '%'+substring(name,b.id,2)+'%' and patindex('%[^吖-座]%',substring(name,b.id,2))=0)
) then 1 else 0 end

return(@re)
end

--测试数据
declare @t table(ID int identity(1,1),loginname varchar(20),password varchar(10))
insert @t select 'haha50005','123'
union all select 'haha50006' ,'465679'
union all select '50006haha' ,'469'
union all select 'の删除记忆…','456'
union all select '美好的记忆”','123456'

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

--结果
/*
ID loginname password
----------- -------------------- ----------
1 haha50005 123
4 の删除记忆… 456

(所影响的行数为 2 行)
*/
lgxyz 2008-09-11
  • 打赏
  • 举报
回复
1 删除记忆
2 删除中国
3 中国asdf

问下这种情况是什么显示

显示
1 删除记忆
2 删除中国

??

34,576

社区成员

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

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