473,425 Members | 1,558 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,425 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 3790
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.