mysql Regular Expression Query for Records Containing Non Numbers and Characters
- 2021-06-28 14:19:24
- OfStack
For example, we have an school table with a field county_name, now let's query county_The name field contains records of characters other than letters and numbers of a-w, so how should sql be written?Look at the following writing:
select * from info where name regexp '[^a-w0-9]';
Regular expressions in mysql use the regexp keyword, [^a-w0-9] to match characters other than letters and numbers in a-w.
Here are some other examples of using mysql regular expressions:
Match name contains all rows of 1000
SELECT * FROM a1 WHERE name regexp '1000'
Matches all lines ending in 000, (.Regular means: matches any 1 character)
SELECT * FROM a1 WHERE name regexp '.000'
Mysql regular case matches, so use the binary keyword for case sensitivity, such as:
SELECT * FROM a1 WHERE name LIKE binary '%J%' # Use LIKE+ Wildcard matches uppercase J
SELECT * FROM a1 WHERE name regexp binary 'j' # Use regular matching lower case j
| is the OR operator of a regular expression, indicating that one of them matches
SELECT * FROM a1 WHERE name regexp binary 'a|j|G'
Matching a specific character, using characters enclosed in [], will match any single character in it.
SELECT * FROM a1 WHERE name regexp '[12]st'
The above'[12]st'regular expression, [12] defines a set of characters that means match 1 or 2
^Matches the beginning of the character
select * from info where name regexp '^L'; // from info surface name Query in fields to L Beginning Record
$end of matching character
select * from info where name regexp 'c$'; // from info surface name Query in fields to c Ending Record
Matches any one character in the string, including carriage return and line break
select * from info where name regexp '^L..y$'; // from info surface name Query in fields to L Start y A record with two arbitrary characters at the end
Match any character in the character set
select * from info where name regexp '[ceo]'; // from info surface name Field Query Contains c , e , o3 Any of the letters 1 Records of
[^Character Set] Matches any character except the character set
SELECT * FROM a1 WHERE name regexp '1000'
0
s1|s2|s3 matches any one of s1s2s3
SELECT * FROM a1 WHERE name regexp '1000'
1
*Represents multiple characters before the character, including 0 or 1
SELECT * FROM a1 WHERE name regexp '1000'
2
+Represents more than one character before the character, including one
select * from info where name regexp 'a+c';// from info surface name Queries in Fields c Previously a Records
N occurrences of string {N}
SELECT * FROM a1 WHERE name regexp '1000'
4
Strings {M, N} occur at least M and at most N
SELECT * FROM a1 WHERE name regexp '1000'
5
MYSQL comes with wildcards (LIKE keywords),%can represent any length of characters (including 0), -can represent a single character
Thank you for reading, I hope to help you, thank you for your support on this site!