如何从 SQL Server 数据库中获取以给定字符串开头的条目?

发布于 2024-09-12 00:53:21 字数 296 浏览 3 评论 0原文

我有一个数据库,其中有很多要在标签系统中使用的单词。我已经为自动完成框创建了必要的代码,但我不确定如何以最有效的方式从数据库中获取匹配的条目。

我知道 LIKE 命令,但在我看来,它更像是 EQUAL 命令。我只得到与我输入的单词完全相同的单词。

我的计划是读取每一行,然后使用 C# 的 string.StartsWith() 和 string.Contains() 函数来查找可能适合的单词,但我认为对于大型数据库,读取每一行和然后过滤它们。

有没有办法从 SQL Server 中只读取以给定字符串开头或包含给定字符串的行?

I have a database with a lot of words to be used in a tag system. I have created the necessary code for an autocomplete box, but I am not sure of how to fetch the matching entries from the database in the most efficient way.

I know of the LIKE command, but it seems to me that it is more of an EQUAL command. I get only the words that looks exactly like the word I enter.

My plan is to read every row, and then use C#'s string.StartsWith() and string.Contains() functions to find words that may fit, but I am thinking that with a large database, it may be inefficient to read every row and then filter them.

Is there a way to read only rows that starts with or contains a given string from SQL Server?

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

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

发布评论

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

评论(4

怀中猫帐中妖 2024-09-19 00:53:21

使用 like 时,您提供 % 符号作为通配符。如果您想要以 Hello 开头的字符串,您将使用 LIKE 'Hello%' 如果您希望在字符串中的任何位置出现带有 Hello 的字符串,您将使用 LIKE '%Hello%'

至于效率,使用 Like 并不是最佳选择。您应该研究全文搜索。

When using like, you provide a % sign as a wildcard. If you want strings that start with Hello, you would use LIKE 'Hello%' If you wanted strings with Hello anywhere in the string, you would use LIKE '%Hello%'

As for efficiency, using Like is not optimal. You should look into full text search.

电影里的梦 2024-09-19 00:53:21

我知道 LIKE 命令,但在我看来,它更像是一个 EQUAL 命令。我只得到与我输入的单词完全相同的单词。

这是因为您没有使用通配符:

WHERE column LIKE 'abc%'

...将返回值以“abc”开头的行。我要指出的是,当使用通配符时,这是唯一可以在...呃列上使用索引的版本。

WHERE column LIKE '%abc%'

...将返回值在其中任意位置包含“abc”的行。对 LIKE 左侧进行通配可确保无法使用索引。

SQL Server 本身并不支持正则表达式 - 您必须使用 CLR 函数才能访问该功能。但它的性能与 LIKE 相当。

全文搜索 (FTS) 是搜索文本的最佳方式。

I know of the LIKE command, but it seems to me that it is more of an EQUAL command. I get only the words that looks exactly like the word I enter.

That's because you aren't using wildcards:

WHERE column LIKE 'abc%'

...will return rows where the column value starts with "abc". I'll point out that when using wildcards, this is the only version that can make use of an index on the column... er column.

WHERE column LIKE '%abc%'

...will return rows where the column value contains "abc" anywhere in it. Wildcarding the left side of a LIKE guarantees that an index can not be used.

SQL Server doesn't natively support regular expressions - you have to use CLR functions to gain access to the functionality. But it performs on par with LIKE.

Full Text Search (FTS) is the best means of searching text.

长伴 2024-09-19 00:53:21

您还可以使用以下语句实现 StartWith 功能:

LEFT('String in wich you search', X) = 'abc'

CHARINDEX('abc', 'String in wich you search') = 1

'String in wich you search' LIKE 'abc%'

使用性能最佳的语句。

You can also implement a StartWith functionality using the following statements:

LEFT('String in wich you search', X) = 'abc'

CHARINDEX('abc', 'String in wich you search') = 1

'String in wich you search' LIKE 'abc%'

Use the one wich performs best.

李白 2024-09-19 00:53:21

您可以在 T-SQL 中使用 CONTAINS,但我非常确定您必须对查询中涉及的表使用全文索引。

包含

全文搜索入门

You can use CONTAINS in T-SQL, but I'm pretty sure you have to have to be using full-text indexing for the table involved in your query.

Contains

Getting started with Full-Text Search

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