简体   繁体   中英

Can you allow a regex match in a MySQL Select statement?

Ok, so generally, when searching for a substring in a table, you can search for something similar using wildcards; ie `'%abc%' will return anything that contains that substring ('abc').

However, if I'm looking for a specific format (perhaps "abc123") can I use a regex match to search for the additional string data? Eg:

cmd.CommandText = "SELECT * FROM [tbl] WHERE [name] LIKE @Param;";

Regex r = "\d*";
cmd.Parameters.AddWithValue("@Param", "abc" + r);

This would then check all of your strings, and depending on the regex, it would compare the strings:

  • 'abc123' - Match
  • 'abcd123' - No match

Post note: I'm not really looking so much into the syntax of the regex right now, I'm more interested to know whether or not this kind of process is possible .

MySQL supports pattern matching operation based on regular expressions and the REGEXP operator.

SELECT name FROM person_tbl WHERE name REGEXP '^st';

You can have a look on the MySQL SELECT LIKE or REGEXP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM