简体   繁体   English

当使用SQL(Teradata)模糊搜索多个单词时,在结果中,如何列出匹配的单词?

[英]When using SQL (Teradata) to fuzzy search multiple words, in the result, how can you list which word is matched?

I want to use like to query some records like: 我想使用像查询一些记录,如:

select table1.survey_txt where table1.survey_txt like any (
'%aaaa%',
'%bbbb%',
'%cccc%',
'%dddd%',
'%eeee%',
'%[^a-z0-9]ffff[^a-z0-9]%'
)

The result will be like: 结果如下:

survey_txt survey_txt

heheaaaahehe heheaaaahehe

kekeaaaakeke kekeaaaakeke

uueabbbbk uueabbbbk

jioewccccfjo jioewccccfjo

esjoffffownr esjoffffownr


So how can I make the result like: 那么如何才能得到如下结果:

survey_txt..........matched_word survey_txt .......... matched_word

heheaaaahehe........aaaa heheaaaahehe ........ AAAA

kekeaaaakeke.........aaaa kekeaaaakeke ......... AAAA

uueabbbbk...............bbbb uueabbbbk ............... BBBB

jioewccccfjo..............cccc jioewccccfjo .............. CCCC

esjoffffownr..................ffff esjoffffownr .................. FFFF


This is a table but I don't know how to make a table here so I just used "....." to make it look like table. 这是一张桌子,但我不知道如何在这里制作一张桌子所以我只是用“.....”使它看起来像桌子。

Use REGEXP_SUBSTR instead of LIKE: 使用REGEXP_SUBSTR而不是LIKE:

SELECT survey_txt,
   RegExp_Substr(survey_txt, '(aaaa|bbbb|cccc|dddd|eeee|[^a-z0-9]ffff[^a-z0-9])') AS matched_word
FROM surveys
WHERE matched_word IS NOT NULL

Try building a table with the search words and like strings, then inner joining. 尝试使用搜索词和类似字符串构建表,然后内部连接。 Since you are using Teradata something like this example should work: 由于您正在使用Teradata,因此此示例应该有效:

CREATE VOLATILE TABLE searchwordslist
  (searchword VARCHAR(32)
  , likestring VARCHAR(32))
ON COMMIT PRESERVE ROWS
;
INSERT INTO searchwordslist VALUES('aaaa', '%aaaa%');
INSERT INTO searchwordslist VALUES('bbbb', '%bbbb%'); 
--and so on for all the words

So now our searchwordlist table looks like: 所以现在我们的searchwordlist表看起来像:

searchword  likestring
bbbb        %bbbb%
aaaa        %aaaa%

Then your query is: 然后你的查询是:

SELECT a.survey_txt 
  , b.searchword
FROM table1 a
INNER JOIN searchwordslist b
  ON a.survey_txt LIKE b.likestring
;

Giving your desired result: 给出您想要的结果:

survey_txt      searchword
heheaaaahehehe  aaaa
kekebbbbkeke    bbbb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要在Teradata SQL中针对单词列表搜索文本字段并返回该单词 - Need to Search a Text Field in Teradata SQL against a list of words and return that word 如何在sql中选择与regexp匹配的单词? - How to select the word which is matched by regexp in sql? Oracle SQL模糊搜索varchar列中的单词 - Oracle SQL fuzzy search for words in a varchar column 需要分隔单词的字符时,如何在MySQL中进行模糊搜索? - How to do fuzzy search in MySQL when separating the characters of the word is needed? 如何在Teradata SQL中用单个空格替换单词之间的多个空格? - How to replace multiple spaces between words with a single space in teradata SQL? 在SQL Server中搜索多个关键字,并根据最大匹配项对结果进行排序 - Search multiple keywords in SQL Server and sort result as per maximum matched Teradata/SQL:如何使用 teradata SQL 比较连续行中的日期并在结果上编写 case 语句 - Teradata/SQL : How to compare dates in consecutive rows using teradata SQL and write case statement on the result 如何在不使用内置函数的情况下在Teradata SQL中获得这种类型的结果 - How to get this type of result in Teradata sql without using inbuilt functions 如何在Teradata SQL中选择多个日期范围? - How can I select multiple date ranges in Teradata SQL? 单词包含错误时如何搜索SQL - How to search SQL when words contain errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM