下面是正在回傳的資料的示例。
| ID | 審查詞 | 說明片段 |
|---|---|---|
| 1 | 肛門 | 肛門 |
| 2 | 肛門 | 手稿提交 |
| 3 | 肛門 | 破傷風疫苗接種 |
| 4 | 肛門 | 海洋提案 |
| 5 | 重新 | 先決條件包括 |
描述片段包含另一個詞或短語內的審查詞,并且可以是多個句子。
當單詞是 anus 并且片段包含單詞破傷風或手稿或海洋時,我想排除回傳的資料,同樣帶有單詞 rere 并且片段包含先決條件。
我在 WHERE 周圍嘗試了各種方法
CensoredWord = 'anus' 和 DescriptionSnippit NOT LIKE '%tetanus%'
OR CensoredWord = 'anus' and DescriptionSnippit NOT LIKE '%manuscript%'
OR CensoredWord = 'anus' and DescriptionSnippit NOT LIKE '%oceanus%'
OR CensoredWord = 'rere' and DescriptionSnippit NOT LIKE '%prerequisite%'
但我來了。這應該是什么樣子?
uj5u.com熱心網友回復:
假設您只是不希望句子包含被審查的單詞,但忽略包含它的單詞。
那么這將適用于大多數 SQL 方言。
但它并不完美。
Fe它不會找到anus!
select *
from test
where concat(' ',description_snippet,' ') not like
concat('% ',censored_word,' %')
一些 RDBMS 具有接受正則運算式的函式。這提供了更多的靈活性。
Fe 字界的使用。
這是一個適用于 Postgresql 的示例
測驗
create table test ( ID serial primary key, censored_word varchar(30), description_snippet varchar(30) ); insert into test (id, censored_word, description_snippet) values (1, 'anus', 'anus') , (2, 'anus', 'manuscript submitted') , (3, 'anus', 'tetanus vaccination') , (4, 'anus', 'oceanus proposal') , (5, 'rere', 'prerequisite includes') , (6, 'rere', 'no rere without anus')
select * from test where description_snippet !~ concat('\m(', censored_word, ')\M')
| ID | censored_word | description_snippet |
|---|---|---|
| 2 | 肛門 | 手稿提交 |
| 3 | 肛門 | 破傷風疫苗接種 |
| 4 | 肛門 | 海洋提案 |
| 5 | 重新 | 先決條件包括 |
db<>在這里擺弄
uj5u.com熱心網友回復:
您可以使用正則運算式搜索在 censored_word 之前或之后至少有一個字母的 description_snippets。
select * from test where lower(description_snippet) regexp lower(concat("[[:alpha:]]",censored_word,"|",censored_word,"[[:alpha:]]"));
或者使用像
select * from test where lower(description_snippet) like (concat('%',lower(censored_word))) or lower(description_snippet) like(concat(lower(censored_word),"%"));
http://sqlfiddle.com/#!9/a471f3/7
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/400986.html
上一篇:根據列上的條件獲取記錄的單個值
下一篇:如何動態創建包含手動資料的表
