[英]How to match any letter in SQL?
I want to return rows where certain fields follow a particular pattern such as whether a particular character in a string is a letter or number. 我想返回某些字段遵循特定模式的行,例如字符串中的特定字符是字母还是数字。 To test it out, I want to return fields where the first letter is any letter.
为了进行测试,我想返回第一个字母是任何字母的字段。 I used this code.
我用了这段代码。
SELECT * FROM sales WHERE `customerfirstname` like '[a-z]%';
It returns nothing. 它什么也不返回。 So I would think that the criteria is the first character is a letter and then any following characters do not matter.
因此,我认为准则是第一个字符是字母,然后任何后续字符都没有关系。 The following code works, but limits rows where the first character is an a.
以下代码有效,但是限制了第一个字符为a的行。
SELECT * FROM sales WHERE `customerfirstname` like 'a%';
Am I not understanding pattern matching? 我不了解模式匹配吗? Isn't it [az] or [AZ] or [0-9] for any letter or number?
是不是字母或数字是[az]或[AZ]或[0-9]? Also if I wanted to run this test on the second character in a string, wouldn't I use
另外,如果我想对字符串中的第二个字符运行此测试,我不会使用
SELECT * FROM `sales` WHERE `customerfirstname` like '_[a-z]%'
This is for SQL and MySQL. 这适用于SQL和MySQL。 I am doing this in phpmyadmin.
我在phpmyadmin中这样做。
You want to use regular expressions: 您要使用正则表达式:
SELECT s.*
FROM sales s
WHERE s.customerfirstname REGEXP '^[a-zA-Z]';
This can be achieved with a regular expression. 这可以通过正则表达式来实现。
SELECT * FROM sales WHERE REGEXP_LIKE(customerfirstname, '^[[:alpha:]]');
^
denotes the start of the string, while the [:alpha]
character class matches any alphabetic character. ^
表示字符串的开头,而[:alpha]
字符类与任何字母字符匹配。
Just in case, here are a few others character classes that you may find useful : 以防万一,这里还有一些其他的字符类,您可能会发现它们有用:
alnum
: dlphanumeric characters alnum
:字母数字字符 digit
: digit characters digit
:数字字符 lower
: lowercase alphabetic characters lower
:小写字母字符 upper
: uppercase alphabetic characters upper
:大写字母字符 See the mysql regexp docs for many more... 有关更多信息,请参见mysql regexp文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.