SQL通配符问题

发布于 2024-10-10 07:55:29 字数 196 浏览 2 评论 0原文

抱歉,我是使用数据库的新手 - 我正在尝试执行查询 这将获取与 SQL 中的字符串相似的所有字符。

例如,

如果我正在查找以某个字符串开头的所有用户,例如 S* 或 Sm* 之类的字符串,它们将返回“Smith、Smelly、Smiles 等...”,

我是否走在正确的轨道上?

任何帮助将不胜感激,并提前致谢!

Sorry I am new to working with databases - I am trying to perform a query
that will get all of the characters that are similar to a string in SQL.

For example,

If I am looking for all users that begin with a certain string, something like S* or Sm* that would return "Smith, Smelly, Smiles, etc..."

Am I on the right track with this?

Any help would be appreciated, and Thanks in advance!

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

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

发布评论

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

评论(6

土豪 2024-10-17 07:55:29

Sql 无法做到这一点...将百分比符号放在中间。

SELECT * FROM users WHERE last_name LIKE 'S%th' 

您需要编写一个 where 子句和一个 and 子句。

 SELECT * FROM users WHERE last_name LIKE 'S%' and last_name LIKE '%th' 

Sql can't do this ... placing the percentage symbol in the middle.

SELECT * FROM users WHERE last_name LIKE 'S%th' 

you would need to write a where clause and an and clause.

 SELECT * FROM users WHERE last_name LIKE 'S%' and last_name LIKE '%th' 
瞎闹 2024-10-17 07:55:29

LIKE 运算符是您要搜索的内容,因此对于您的示例,您需要类似以下内容:

SELECT * 
  FROM [Users]
 WHERE LastName LIKE 'S%'

% 字符是 SQL 中的通配符。

The LIKE operator is what you are searching for, so for your example you would need something like:

SELECT * 
  FROM [Users]
 WHERE LastName LIKE 'S%'

The % character is the wild-card in SQL.

貪欢 2024-10-17 07:55:29

标准 (ANSI) SQL 有两个与 LIKE 关键字一起使用的通配符:

  • _(下划线)。匹配任何单个字符的一次出现。
  • %(百分号)。匹配任意单个字符零次或多次出现。

此外,SQL Server 扩展了 LIKE 通配符匹配以包含字符集规范,这与普通正则表达式字符集说明符非常相似:

  • [character-set] 匹配指定集中的单个字符
  • [ ^character-set] 匹配不在指定集合中的单个字符。

字符集也可以按正常方式指定为范围:

  • [0-9] 匹配任何十进制数字。
  • [AZ] 匹配任何大写字母
  • [^A-Z0-9-] 匹配任何非字母、数字或连字符的字符。

当然,字母匹配的语义取决于所使用的排序规则序列。它可能区分大小写,也可能不区分大小写。

此外,要匹配文字左方括号 ('[]'),您必须使用字符范围说明符。您不会收到语法错误,但也不会收到匹配项。

where x.field like 'x[[][0-9]]'

将匹配看起来像 'x[0]' 、 'x[8]' 等的文本。但

where 'abc[x' like 'abc[x'

始终为 false。

Standard (ANSI) SQL has two wildcard characters for use with the LIKE keyword:

  • _ (underscore). Matches a single occurrence of any single character.
  • % (percent sign). Matches zero or more occurrences of any single character.

In addition, SQL Server extends the LIKE wildcard matching to include character set specification, rather like a normal regular expresion character set specifier:

  • [character-set] Matches a single character from the specified set
  • [^character-set] Matches a single character not in the specified set.

Character sets may be specified in the normal way as a range as well:

  • [0-9] matches any decimal digit.
  • [A-Z] matches any upper-case letter
  • [^A-Z0-9-] matches any character that isn't a letter, digit or hyphen.

The semantics of letter matching of course, a dependent on the collation sequence in use. It may or may not be case-sensitive.

Further, to match a literal left square bracket ('[]'), you must use the character range specifier. You won't get a syntax error, but you won't get a match, either.

where x.field like 'x[[][0-9]]'

will match text that looks like 'x[0]' , 'x[8]', etc. But

where 'abc[x' like 'abc[x'

will always be false.

满地尘埃落定 2024-10-17 07:55:29

获取姓氏为 smith 的所有用户

SELECT * 
  FROM [Users]
 WHERE LastName ='Smith'

获取姓氏包含 smith 的所有用户 执行此操作,这还将返回 blasmith、smith2 等

SELECT * 
  FROM [Users]
 WHERE LastName LIKE '%Smith%'

如果您想要以 smith 开头的所有内容,请执行此操作

SELECT * 
  FROM [Users]
 WHERE LastName LIKE 'Smith%'

to get all the users with a lastname of smith

SELECT * 
  FROM [Users]
 WHERE LastName ='Smith'

to get all users where the lastname contains smith do this, that will also return blasmith, smith2 etc etc

SELECT * 
  FROM [Users]
 WHERE LastName LIKE '%Smith%'

If you want everything that starts with smith do this

SELECT * 
  FROM [Users]
 WHERE LastName LIKE 'Smith%'
吃→可爱长大的 2024-10-17 07:55:29

您可能还喜欢 SOUNDEX 的结果,具体取决于您对姓氏相似度的偏好。

select * 
from [users]
where soundex('lastname') = soundex( 'Smith' )
or upper(lastname) like 'SM%'

you might also like the results of SOUNDEX, depending on your preference for last name similarity.

select * 
from [users]
where soundex('lastname') = soundex( 'Smith' )
or upper(lastname) like 'SM%'
够钟 2024-10-17 07:55:29

你的问题并不完全清楚。

如果您想要所有姓氏为 Smith 的用户,则可以使用常规搜索:

 SELECT * FROM users WHERE last_name = 'Smith'

如果您想要所有以 'S' 开头并以 'th' 结尾的用户,您可以使用 LIKE 和通配符:(

 SELECT * FROM users WHERE last_name LIKE 'S%th'

注意标准 SQL 许多通配符'%' 而不是 '*')。

如果您确实想要“听起来像”匹配,许多数据库都支持一种或多种 SOUNDEX 算法搜索。检查您的特定产品的文档(您在问题中没有提及)。

Your question isn't entirely clear.

If you want all the users with last name Smith, a regular search will work:

 SELECT * FROM users WHERE last_name = 'Smith'

If you want all the users beginning with 'S' and ending in 'th', you can use LIKE and a wildcard:

 SELECT * FROM users WHERE last_name LIKE 'S%th'

(Note the standard SQL many wildcard of '%' rather than '*').

If you really want a "sounds like" match, many databases support one or more SOUNDEX algorithm searches. Check the documentation for your specific product (which you don't mention in the question).

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