SQL通配符问题
抱歉,我是使用数据库的新手 - 我正在尝试执行查询 这将获取与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Sql 无法做到这一点...将百分比符号放在中间。
您需要编写一个 where 子句和一个 and 子句。
Sql can't do this ... placing the percentage symbol in the middle.
you would need to write a where clause and an and clause.
LIKE
运算符是您要搜索的内容,因此对于您的示例,您需要类似以下内容:%
字符是 SQL 中的通配符。The
LIKE
operator is what you are searching for, so for your example you would need something like:The
%
character is the wild-card in SQL.标准 (ANSI) SQL 有两个与 LIKE 关键字一起使用的通配符:
此外,SQL Server 扩展了 LIKE 通配符匹配以包含字符集规范,这与普通正则表达式字符集说明符非常相似:
字符集也可以按正常方式指定为范围:
当然,字母匹配的语义取决于所使用的排序规则序列。它可能区分大小写,也可能不区分大小写。
此外,要匹配文字左方括号 ('[]'),您必须使用字符范围说明符。您不会收到语法错误,但也不会收到匹配项。
将匹配看起来像 'x[0]' 、 'x[8]' 等的文本。但
始终为 false。
Standard (ANSI) SQL has two wildcard characters for use with the LIKE keyword:
In addition, SQL Server extends the LIKE wildcard matching to include character set specification, rather like a normal regular expresion character set specifier:
Character sets may be specified in the normal way as a range as well:
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.
will match text that looks like 'x[0]' , 'x[8]', etc. But
will always be false.
获取姓氏为 smith 的所有用户
获取姓氏包含 smith 的所有用户 执行此操作,这还将返回 blasmith、smith2 等
如果您想要以 smith 开头的所有内容,请执行此操作
to get all the users with a lastname of smith
to get all users where the lastname contains smith do this, that will also return blasmith, smith2 etc etc
If you want everything that starts with smith do this
您可能还喜欢
SOUNDEX
的结果,具体取决于您对姓氏相似度的偏好。you might also like the results of
SOUNDEX
, depending on your preference for last name similarity.你的问题并不完全清楚。
如果您想要所有姓氏为 Smith 的用户,则可以使用常规搜索:
如果您想要所有以 'S' 开头并以 'th' 结尾的用户,您可以使用 LIKE 和通配符:(
注意标准 SQL 许多通配符'%' 而不是 '*')。
如果您确实想要“听起来像”匹配,许多数据库都支持一种或多种 SOUNDEX 算法搜索。检查您的特定产品的文档(您在问题中没有提及)。
Your question isn't entirely clear.
If you want all the users with last name Smith, a regular search will work:
If you want all the users beginning with 'S' and ending in 'th', you can use LIKE and a wildcard:
(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).