473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to search a pattern in MySQL ?

296 100+
Hi all of you !!

I have a column in a table which has data seperated by spaces like this -->
AS DD QW-02-44 WE
FG QW-02-44 RE WE
QW WQ

I am implementing a search functionality wherein a user will enter some data in a textbox to search.
For eg:- if the user wants to retreive rows which have WQ in them then the result should be 3rd row above.

How to do this ?

Platform --> Debian Sarge
Language --> Perl CGI
Dec 26 '06 #1
11 3789
ronverdonk
4,258 Expert 4TB
There are different ways of doing this, such as using the LIKE attrib or a regular expression. The following samples show each method (I personnaly favor the regular expression method).
Expand|Select|Wrap|Line Numbers
  1. $search = 'WQ';
  2. $sql1 = 'SELECT * FROM table ';
  3. $sql1.= 'WHERE column REGEXP "' . $search . '"';
  4.  
  5. or  
  6.  
  7. $sql2 = 'SELECT * FROM table ';
  8. $sql2.= 'WHERE column LIKE "%' . $search . '%"';
  9.  
Ronald :cool:
Dec 26 '06 #2
pankajit09
296 100+
There are different ways of doing this, such as using the LIKE attrib or a regular expression. The following samples show each method (I personnaly favor the regular expression method).
Expand|Select|Wrap|Line Numbers
  1. $search = 'WQ';
  2. $sql1 = 'SELECT * FROM table ';
  3. $sql1.= 'WHERE column REGEXP "' . $search . '"';
  4.  
  5. or  
  6.  
  7. $sql2 = 'SELECT * FROM table ';
  8. $sql2.= 'WHERE column LIKE "%' . $search . '%"';
  9.  
Ronald :cool:

Suppose I want to search WQ QW(random) then also I want the 3rd row .
How to do that ?
Dec 26 '06 #3
ronverdonk
4,258 Expert 4TB
Depends on whether you want to test for one string 'WQ QW' or for 2 separate strings 'WQ' and 'QW'.
Expand|Select|Wrap|Line Numbers
  1. $search1 = 'WQ';
  2. $search2 = 'QW';
  3. $search3 = 'WQ QW';
  4.  
  5. // search for 1 string 'WQ QW' 
  6. $sqla  = 'SELECT * from table ';
  7. $sqla .= 'WHERE column REGEXP "' . search3 . '" ';
  8.  
  9. // search for 2 strings 'WQ' and 'QW' 
  10. $sqlb  = 'SELECT * from table ';
  11. $sqlb .= 'WHERE column REGEXP "' . search1 . '" ';
  12. $sqlb .= 'AND column REGEXP "' . $search2 . '" ';
  13.  
Ronald :cool:
Dec 26 '06 #4
pankajit09
296 100+
Depends on whether you want to test for one string 'WQ QW' or for 2 separate strings 'WQ' and 'QW'.
Expand|Select|Wrap|Line Numbers
  1. $search1 = 'WQ';
  2. $search2 = 'QW';
  3. $search3 = 'WQ QW';
  4.  
  5. // search for 1 string 'WQ QW' 
  6. $sqla  = 'SELECT * from table ';
  7. $sqla .= 'WHERE column REGEXP "' . search3 . '" ';
  8.  
  9. // search for 2 strings 'WQ' and 'QW' 
  10. $sqlb  = 'SELECT * from table ';
  11. $sqlb .= 'WHERE column REGEXP "' . search1 . '" ';
  12. $sqlb .= 'AND column REGEXP "' . $search2 . '" ';
  13.  
Ronald :cool:

Thanks Ronald for that.
This is what I wanted.

I like the smiley beside your name . :-D
Dec 27 '06 #5
ronverdonk
4,258 Expert 4TB
Glad I could help you out.

Ronald :cool:
Dec 27 '06 #6
pankajit09
296 100+
One more problem -->

*europe should match "DD EASTEUROPE" and not
"WE EUROPE" (ignore spaces).

How to do this ?
Dec 30 '06 #7
pankajit09
296 100+
ok no need to think I got -->

$textbox =~ s/\s/[^[:space:]]/g;

$sqla = 'SELECT * from table ';
$sqla .= 'WHERE column REGEXP "$textbox" ';
Dec 30 '06 #8
Hi Friends,
i am having one trigger with all insert ,update and delete opreations, then how to call this trigger from user form.

environment is :
ASP.NET with C#
Sql Server

kindly help me inthis regard.

thanks
viswanath.
Dec 30 '06 #9
ronverdonk
4,258 Expert 4TB
viswanatareddy:

You are

(a) in the wrong thread (this thread has nothing to do with triggers).
(b) in the wrong forum (post your question in SQL server or .NET forum)

Ronald :cool:
Dec 30 '06 #10
pankajit09
296 100+
Ronald,

may i know why you personally favour the regular expression method ?
Jan 2 '07 #11
ronverdonk
4,258 Expert 4TB
Ronald,

may i know why you personally favour the regular expression method ?
The main reason why I prefer a REGEXP above the LIKE is that it gives more flexibility as opposed to a more rigid matching with LIKE. These reasons are (MySQL doc):
  • classes: in my opinion the most important one. A character class ‘[...]’ matches any character within the brackets.
    For example, ‘[abc]’ matches ‘a’, ‘b’, or ‘c’. To name a range of characters, use a dash. ‘[a-z]’ matches any letter, whereas ‘[0-9]’ matches any digit.
  • no of instances: Also important to me. ‘*’ matches zero or more instances of the thing preceding it.
    For example, ‘x*’ matches any number of ‘x’ characters, ‘[0-9]*’ matches any number of digits, and ‘.*’ matches any number of anything.
  • position: a REGEXP pattern match succeeds if the pattern matches anywhere in the value being tested. (This differs from a LIKE pattern)

See also the MySQL documentation on Regular Expressions

Ronald :cool:
Jan 2 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Sharif T. Karim | last post by:
I am trying to do the following with my search script that looks for records in a mysql table. The following is an example of what I am trying to do. Text being searched: -- The brown fox...
5
by: Vamsee Krishna Gomatam | last post by:
Hello, I'm having some problems understanding Regexps in Python. I want to replace "<google>PHRASE</google>" with "<a href=http://www.google.com/search?q=PHRASE>PHRASE</a>" in a block of text....
3
by: Andrew Crowe | last post by:
Hi guys, I have a table currently set up like this: <- -> video ------ video_id
22
by: Phlip | last post by:
C++ers: Here's an open ended STL question. What's the smarmiest most templated way to use <string>, <algorithms> etc. to turn this: " able search baker search charlie " into this: " able...
2
by: Alphonse Giambrone | last post by:
Is there a way to use multiple search patterns when calling Directory.GetFiles. For instance Directory.GetFiles("C:\MyFolder", "*.aspx") will return all files with the aspx extension. But what if...
2
by: news | last post by:
We're trying to make a search form, and are wanting to create links for all the letters a person can click on and will bring up all entries that start with that letter. That part is easy: .......
1
by: jrs_14618 | last post by:
Hello All, This post is essentially a reply a previous post/thread here on this mailing.database.myodbc group titled: MySQL 4.0, FULL-TEXT Indexing and Search Arabic Data, Unicode I was...
3
by: FluffyCat | last post by:
Last month I continued my series of design patterns examples using PHP 5 with the Observer Pattern and the Prototype Pattern. Here now is my 16th example, the Adapter pattern. ...
1
by: Eric | last post by:
Hi: I have two files. I search pattern ":" from emails text file and save email contents into a database. Another search pattern " field is blank. Please try again.", vbExclamation + vbOKOnly...
4
by: | last post by:
Hello... i have a table which contains a column named "ask" and a column named "per"... my think is that i want to search in "ask" and echo the data stored in "per" for this entry... How do i do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.