仅与 LIKE 匹配整个单词?

发布于 2024-11-14 16:20:01 字数 149 浏览 3 评论 0原文

所以 'awesome document' LIKE '%doc%' 是 true,因为 doc 是一个子字符串。但是,我希望它是假的,而“很棒的文档”或“很棒的文档”或“很棒的文档”应该是真的。我该怎么办?

我正在使用 sqlite,所以我希望不必使用不可用的东西。

So 'awesome document' LIKE '%doc%' is true, because doc is a sub-string. But, I want it to be false while 'awesome doc' or 'doc awesome' or 'awesome doc awesome' should be true. How can I do with with a like?

I'm using sqlite, so I hope I don't have to use something that isn't available.

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

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

发布评论

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

评论(9

鱼窥荷 2024-11-21 16:20:01

如何将其分成四个部分 -

[MyColumn] Like '% doc %' 
OR [MyColumn] Like '% doc' 
OR [MyColumn] Like 'doc %' 
OR [MyColumn] = 'doc'

编辑:另一种方法(仅适用于 ascii 字符)可能是:(

'#'+[MyColumn]+'#' like '%[^a-z0-9]doc[^a-z0-9]%'

您可能还想处理任何特殊字符)

它看起来不像,但您可能想探索 < a href="http://msdn.microsoft.com/en-us/library/ms142547.aspx" rel="noreferrer">全文搜索 和 包含,以防更适合您情况。

看:
- MSDN:[ ](通配符 - 字符) 匹配) (Transact-SQL)

How about split it into four parts -

[MyColumn] Like '% doc %' 
OR [MyColumn] Like '% doc' 
OR [MyColumn] Like 'doc %' 
OR [MyColumn] = 'doc'

Edit: An alternate approach (only for ascii chars) could be:

'#'+[MyColumn]+'#' like '%[^a-z0-9]doc[^a-z0-9]%'

(You may want to take care of any special char as well)

It doesn't look like, but you may want to explore Full Text Search and Contains, in case that's more suitable for your situation.

See:
- MSDN: [ ] (Wildcard - Character(s) to Match) (Transact-SQL)

纵性 2024-11-21 16:20:01

您可以只使用以下条件:

(' '+YOUR_FIELD_NAME+' ') like '% doc %'

它比其他解决方案工作得更快。

you can just use below condition:

(' '+YOUR_FIELD_NAME+' ') like '% doc %'

it works faster than other solutions.

怂人 2024-11-21 16:20:01
WHERE (@string LIKE 'doc %' OR @string LIKE '% doc' OR @string LIKE '% doc %' OR @string = 'doc')
WHERE (@string LIKE 'doc %' OR @string LIKE '% doc' OR @string LIKE '% doc %' OR @string = 'doc')
羁〃客ぐ 2024-11-21 16:20:01

您可以使用一些 OR,LIKE '% doc %' OR LIKE 'doc %' OR LIKE '% doc' OR LIKE 'doc'

You could use some ORs, LIKE '% doc %' OR LIKE 'doc %' OR LIKE '% doc' OR LIKE 'doc'

<逆流佳人身旁 2024-11-21 16:20:01

LIKE 相当有限。我建议 REGEXP,例如:

 columnName REGEXP 'doc[;.?!,") ]'

例如:awesome document 不匹配,但是
很棒的文档
文档太棒了,
Awesome doc Awesome 将匹配。

它还将匹配文本中的现有单词,后跟标点符号。
您可以在此处尝试任何示例:https://regexr.com/

LIKE is quite limited. I would suggest REGEXP, for example:

 columnName REGEXP 'doc[;.?!,") ]'

for example: awesome document wouldn't match, but
awesome doc,
doc awesome,
awesome doc awesome will match.

It will also match an existing word in text followed by punctuation characters.
You can try any example here: https://regexr.com/

如梦初醒的夏天 2024-11-21 16:20:01

在更现代的 SQL Server 中,您可以使用 STRING_SPLIT 函数轻松完成此操作。

以下链接因空格而分开。

SELECT * FROM DataTable T WHERE 'doc' IN (SELECT VALUE FROM STRING_SPLIT(T.TextField, ' '))

如果您想包含逗号、句号、问号、分号和感叹号,即“,.?;!”您可以添加翻译。

SELECT * FROM DataTable T WHERE 'doc' IN (SELECT VALUE FROM STRING_SPLIT(TRANSLATE(T.TextField, ',.?;!', '     '), ' '))

In more modern SQL Servers you can use the STRING_SPLIT function to do this easily.

The following link splits on spaces.

SELECT * FROM DataTable T WHERE 'doc' IN (SELECT VALUE FROM STRING_SPLIT(T.TextField, ' '))

If you wanted to include commas, periods, question marks, semicolons and exclamation points ie ",.?;!" you can add a translate.

SELECT * FROM DataTable T WHERE 'doc' IN (SELECT VALUE FROM STRING_SPLIT(TRANSLATE(T.TextField, ',.?;!', '     '), ' '))
浪菊怪哟 2024-11-21 16:20:01

所有答案都不适合我(SQL Server 2008 R2)。

但我设法使用下面显示的一些答案让它工作。

SELECT * FROM myTable AS v
WHERE v.Note LIKE '%[^a-zA-Z0-9]CONTainers[^a-zA-Z0-9]%'

注意: v.Note 是一种文本数据类型

这对我来说适用于以下示例..

  • 容器
  • CONTAINERS
  • conTaInERS

但不是当“容器”一词被其他字母或数字包围。

  • 001containers001
  • AAcontainersAA

这是OP首先要求的。

None of the answers worked for me (SQL Server 2008 R2).

But I managed to get it to work using somewhat of the answers shown below..

SELECT * FROM myTable AS v
WHERE v.Note LIKE '%[^a-zA-Z0-9]CONTainers[^a-zA-Z0-9]%'

Note: v.Note is a TEXT data type

This worked for me with the following samples..

  • containers
  • CONTAINERS
  • conTaInERS

But not when the word 'containers' was surrounded by other letters or numerics..

  • 001containers001
  • AAcontainersAA

Which is what the OP asked for in the first place.

Smile简单爱 2024-11-21 16:20:01

您可以将 REGEX 与“单词边界”一起使用 MySql 文档

所以,在您的示例中:“很棒的文档”REGEXP“\bdoc\b”

You can use REGEX with "word boundary" MySql documentation

So, in your example : 'awesome document' REGEXP '\bdoc\b'

满栀 2024-11-21 16:20:01

不喜欢“%doc%”怎么样?

What about NOT LIKE '%doc%'?

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