如何删除数据库中的特定单词?

How to remove specific words in database?

我会尝试用解释来解释,因为有点 complicated.I 想从我的 SQL table 中删除单词,如果单词的词根已经存在于 table.所以我的table结构是这样的

Words  Scores
car       5
book      11
cars       2
pen        10
tool       4
car's      8
tools      2

所以在这种情况下,由于 car 是 car's 和 cars 的词根,tool 是 tools 的词根,我想删除它们并将它们的分数添加到词根上,这样得到我的 table;

Words  Scores
car       15
book      11  
pen       10
tool       6

不是:如果列表中有 "a",那么所有以 a 开头的单词都将被删除:为此我考虑了 if 条件; if (string[i].Length>=3) 但当然它不会避免所有可能性,也许会有 "book" 和 "booking" 这样的词,所以预订将被删除,但没关系。

嗯,如果你没有包含根和 child 的关系 table,我想不出更简单的解决方案,但你可以尝试这样的事情 - 分两步, 第一步是更新分数,第二步是删除 childs :

UPDATE YourTable t
SET t.Scores =(SELECT sum(s.scores) FROM YourTable s
               WHERE s.words like concat('%',t.words,'%'))

编辑: 或这个

update t
set t.score=sum(s.score) 
from YourTable t
INNER JOIN YourTable s
 ON (s.words like concat('%',t.words,'%'))

这会将每个人更新到他们的 childs(看起来相似)总分。

然后删除:

DELETE FROM YourTable t
WHERE t.words in(SELECT s.words FROM YourTable s
                 WHERE t.words like concat('%',s.words,'%')
                 AND t.words <> s.words)

这将删除与另一个词child(相似)的所有记录。它不适用于任何 DBMS,所以这是它的另一个版本,它带有一个连接(更新连接语法从一个数据库到另一个数据库是不同的):

DELETE FROM YourTable t
INNER JOIN YourTable s
 ON(t.words like concat('%',s.words,'%')
    AND t.words <> s.words)

您没有提供 RDBMS,因此这是对 ANSI-SQL 的回答。 这是未经测试的,所以请检查它是否有效。

编辑: 请记住,如果没有 root-child table,将会出现一些无法正常工作的异常,并可能导致不必要的 update/deletion.你必须制定规则,当一个词是另一个词的 child 时,这将没有期望(我不知道它是否可能使用 sql)。

我最好的建议 - 自己填充 table,插入所有 root-child 选项,并将 table 用于 delete/update ,这将确保不会出错将制作。

这是一个发现一些常见情况的开始。作为第一步,只考虑 3 个或更多字符的单词是合理的。

select distinct w2.word from words w inner join words w2
on w.word = w2.word + 's'
    or w.word = w2.word + '''s'
    or w.word = w2.word + 'ing'
    or w.word = w2.word + substring(w2.word, len(w2.word), 1) + 'ed'    
    or w.word = w2.word + substring(w2.word, len(w2.word), 1) + 'y'
where len(w.word) >= 3

删除派生词:

delete w from words w inner join words w2
on w.word = w2.word + 's'
    or w.word = w2.word + '''s'
    or w.word = w2.word + 'ing'
    or w.word = w2.word + substring(w2.word, len(w2.word), 1) + 'ed'    
    or w.word = w2.word + substring(w2.word, len(w2.word), 1) + 'y'
where len(w2.word) >= 3

要计算字数,您可以这样做: 我确信有一种更优雅的方法可以做到这一点,并且会在我找到一个时更新此 post。 首先添加一个视图,或者如果您不能这样做,请创建一个临时文件 table #root_words 并将以下内容插入其中。

create  view root_words as
select distinct w2.word as root_word, w.word as derived_word 
from words w inner join words w2
on w.word = w2.word + 's'
    or w.word = w2.word + '''s'
    or w.word = w2.word + 'ing'
    or w.word = w2.word + substring(w2.word, len(w2.word), 1) + 'ed'    
    or w.word = w2.word + substring(w2.word, len(w2.word), 1) + 'y'
where len(w2.word) >= 3 

然后此查询执行 union 以包含像 "dog" 这样的词,这些词不是从任何其他词派生的。否则他们将被忽略。

select x.root_word, count(*) 
from 
(
    select rw.root_word, rw.derived_word
    from words w
    inner join root_words rw on w.word = rw.root_word
    -- add words which aren't derived from any other word
    union all
    select w.word as root_word, null as derived_word
    from words w
    left join root_words rw on w.word = rw.derived_word
    where rw.root_word is null
) x
group by x.root_word