使用自定义条件删除表中的相似记录

发布于 2024-10-07 01:57:33 字数 253 浏览 1 评论 0原文

我在 sql server 中有一个表,在我的表中有一个类似 name 的字段。 我需要从我的表中删除具有这种条件的记录。 除了最后一个字符外,所有字符都是相似的,最后一个字符是“a”或“b”

例如表记录是name1aname2aname1b,.... 并且需要删除 name1aname1b

I have a table in sql server and in my table is a field like name.
I need to delete records from my table with this condition.
All characters are similar except for the last character, and last character is "a" or "b"

For example table records are name1a,name2a,name1b,....
and need delete name1a and name1b

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

英雄似剑 2024-10-14 01:57:33

如果您询问如何删除表中具有重复项的任何行(匹配除最后一个字符之外的所有行)而不是如何使用通配符,则方法如下:

declare @names table (keycol int, namecol varchar(10))

insert into @names (keycol, namecol) values (1, 'name1a')
insert into @names (keycol, namecol) values (2, 'name1b')
insert into @names (keycol, namecol) values (3, 'name2a')
insert into @names (keycol, namecol) values (4, 'name2b')
insert into @names (keycol, namecol) values (5, 'name3a')
insert into @names (keycol, namecol) values (6, 'name4b')

;with dupenames as
(
    select LEFT(namecol,LEN(namecol)-1) as NameMinusOne
    from @names
    group by LEFT(namecol,LEN(namecol)-1)
    having count(*) > 1
)
delete from @names
from @names n
inner join dupenames dn on dn.NameMinusOne = LEFT(n.namecol,LEN(n.namecol)-1)

输出:

keycol      namecol
----------- ----------
5           name3a
6           name4b

If you are asking how to delete any rows that have duplicates in the table (matching all but the last character) rather than how to use a wildcard, here is how:

declare @names table (keycol int, namecol varchar(10))

insert into @names (keycol, namecol) values (1, 'name1a')
insert into @names (keycol, namecol) values (2, 'name1b')
insert into @names (keycol, namecol) values (3, 'name2a')
insert into @names (keycol, namecol) values (4, 'name2b')
insert into @names (keycol, namecol) values (5, 'name3a')
insert into @names (keycol, namecol) values (6, 'name4b')

;with dupenames as
(
    select LEFT(namecol,LEN(namecol)-1) as NameMinusOne
    from @names
    group by LEFT(namecol,LEN(namecol)-1)
    having count(*) > 1
)
delete from @names
from @names n
inner join dupenames dn on dn.NameMinusOne = LEFT(n.namecol,LEN(n.namecol)-1)

OUTPUT:

keycol      namecol
----------- ----------
5           name3a
6           name4b
魔法少女 2024-10-14 01:57:33

我想您是在问如何使用通配符来识别(和删除)某些行?

在 sql server 中,单字符通配符是“_”,因此如果您想删除以“name1”开头并具有单个字符后缀的行,这种事情会起作用:

declare @names table (keycol int, namecol varchar(10))

insert into @names (keycol, namecol) values (1, 'name1a')
insert into @names (keycol, namecol) values (2, 'name1b')
insert into @names (keycol, namecol) values (3, 'name2a')
insert into @names (keycol, namecol) values (4, 'name2b')
insert into @names (keycol, namecol) values (5, 'name3a')
insert into @names (keycol, namecol) values (6, 'name3b')

delete from @names where namecol like 'name1_'

如果“name1”部分之后有更多字符,您将使用:

delete from @names where namecol like 'name1%'

I think you are asking about how to use wild cards to identify (and delete) certain rows?

In sql server the single character wildcard is '_' so if you want to remove rows that start 'name1' and have a single character suffix this kind of thing will work:

declare @names table (keycol int, namecol varchar(10))

insert into @names (keycol, namecol) values (1, 'name1a')
insert into @names (keycol, namecol) values (2, 'name1b')
insert into @names (keycol, namecol) values (3, 'name2a')
insert into @names (keycol, namecol) values (4, 'name2b')
insert into @names (keycol, namecol) values (5, 'name3a')
insert into @names (keycol, namecol) values (6, 'name3b')

delete from @names where namecol like 'name1_'

If there are more characters after the 'name1' part you would use:

delete from @names where namecol like 'name1%'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文