• Subcribe to Our RSS Feed

[MySql]Search Pattern With Regular Exression In MySql Using “RLIKE”

May 5, 2012   //   by admin   //   Mysql, PHP  //  No Comments

Hi Friends,
I think you are familiar with regular expression very well.Did you know
MySql also support searching with regular expression.Using regular expression
you can also search pattern like email pattern.

Let we see syntax for searching using regular expression in MySql.

      $query="SELECT * FROM table WHERE column RLIKE regular expression";

Now i explain with an example:

Search With Only Letter

In example of search with only letter which column content should be only
a-z or A-Z.

      $query="SELECT * FROM tbl_students WHERE first_name RLIKE '[a-z A-Z]'";

First One Must Be Latter

In example of first one must be latter which column content should be only
[a-z or A-Z]%.

      $query="SELECT * FROM tbl_students WHERE id_number RLIKE '[a-z A-Z]%'";

Search With Email Pattern

In example of search with email pattern which column content should be only
valid email.

      $query="SELECT * FROM tbl_students WHERE email RLIKE '^[-0-9a-z_\\.]+@[-0-9a-z_\\.]+\\.[a-z]{2,3}$'";

Using above technique for searching with regular expression in mysql.
You can get your expected result.
Thanks.
All The Best.

Leave a comment

*

Social Media Auto Publish Powered By : XYZScripts.com