Current location: Hot Scripts Forums » Other Discussions » Database » Database select querry


Database select querry

Reply
  #11 (permalink)  
Old 11-25-09, 06:24 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Instead of using REPLACE on the WHERE - you might want to use REGEXP.

MySQL :: MySQL 5.0 Reference Manual :: 11.4.2 Regular Expressions

In this case, you could try:

$sQuery=preg_replace("[^A-Za-z0-9]", "", $entry);

with

... WHERE `model` REGEXP '.$sQuery ...
Reply With Quote
  #12 (permalink)  
Old 11-25-09, 09:46 AM
alane alane is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 71
Thanks: 6
Thanked 0 Times in 0 Posts
So are you suggesting changing the WHERE statemant to

PHP Code:

WHERE

     
`modelREGEXP '.$sQuery like '%$entry%') AND class = 1 
and defining
PHP Code:

$sQuery=preg_replace("[^A-Za-z0-9]"""$entry); 

before the SELECT statement?
Reply With Quote
  #13 (permalink)  
Old 11-25-09, 10:42 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I looked into REGEXP and thought it may be of use in this situation.
But REGEXP is just another way of saying RLIKE.

And that doesn't help remove the characters from the data that's in the database for use in the WHERE clause.

You can't say:
PHP Code:

WHERE model REGEXP '[^A-Za-z0-9]' LIKE '%$entry%' 

You can say:
PHP Code:

WHERE model REGEXP '$entry'

or

 
WHERE model RLIKE '$entry'

or

WHERE model LIKE '%$enter%' 
But none of those solves the problem of stripping the unwanted characters from the data in the database before using it in the WHERE clause.
__________________
Jerry Broughton
Reply With Quote
  #14 (permalink)  
Old 11-25-09, 11:30 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Good point.

In that case, the regular expression needs to allow the entered string to be more tolerant.

Thus - if you modify the regular expression to allow characters between those entered, it should match.

This code is for phone numbers, but it may be helpful here, too. Instead of replacing all the digits in the string, you could replace every character.

PHP Code:



<?php 
/* A list of phone numbers to test for matches */
 
$aPhoneNumbers=array( 
 
'7085556232'
 
'1(708)555-6232'
 
'(708)555-6232'
 
'708.555.6232'
 
'1.708.555.6232'
 
'1.708.555.6232 ext. 123'
 
'17085556232'
 
'7215556232'
 
'1(721)555-6232'
 
'(721)555-6232'
 
'721.555.6232'
 
'1.721.555.6232'
 
'17215556232' 
 
); 
   
/* sPhone is the phone number */
 
$sPhone='7085556232'

/* Pattern is all digits */
 
$rPattern='/(\d)/'

/* Replace all non-digits */
 
$rReplace='\D*${1}'

/* Create the regular expression */
 
$sRegExp='/'.preg_replace($rPattern,$rReplace,$sPhone).'/'
   
/* Display it */
 
echo 'Regular Expression: '.$sRegExp."\n"
   
/* Run the test on all the input */
 
foreach ($aPhoneNumbers as $k => $v
         
check($sRegExp,$v); 
  
/* Test function */\
 function 
check($sRegExp,$sPhone
 { 
 echo 
'$sPhone: '.$sPhone.' ? '.preg_match($sRegExp,$sPhone)."\n"
 } 
 
?>
Although I used PHP to develop the regexp logic, I was able to convert it to use the MySQL equivalents - like :digit:, etc.

Output

Quote:
Regular Expression: /.?7.?0.?8.?5.?5.?5.?6.?2.?3.?2/
$sPhone: 7085556232 ? 1
$sPhone: 1(708)555-6232 ? 1
$sPhone: (708)555-6232 ? 1
$sPhone: 708.555.6232 ? 1
$sPhone: 1.708.555.6232 ? 1
$sPhone: 1.708.555.6232 ext. 123 ? 1
$sPhone: 17085556232 ? 1
$sPhone: 7215556232 ? 0
$sPhone: 1(721)555-6232 ? 0
$sPhone: (721)555-6232 ? 0
$sPhone: 721.555.6232 ? 0
$sPhone: 1.721.555.6232 ? 0
$sPhone: 17215556232 ? 0
The .? indicates any single character. You may want to adjust that to [.*]? - meaning any string of characters, or something more specific.

Output doesn't match the code - but - the general idea is to intersperse the target characters with others such that it will match.

Last edited by wirehopper; 11-25-09 at 11:32 AM.
Reply With Quote
  #15 (permalink)  
Old 11-25-09, 12:28 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by wirehopper View Post
Good point.

In that case, the regular expression needs to allow the entered string to be more tolerant.

Thus - if you modify the regular expression to allow characters between those entered, it should match.

This code is for phone numbers, but it may be helpful here, too. Instead of replacing all the digits in the string, you could replace every character.

PHP Code:


<?php 
/* A list of phone numbers to test for matches */
 
$aPhoneNumbers=array( 
 
'7085556232'
 
'1(708)555-6232'
 
'(708)555-6232'
 
'708.555.6232'
 
'1.708.555.6232'
 
'1.708.555.6232 ext. 123'
 
'17085556232'
 
'7215556232'
 
'1(721)555-6232'
 
'(721)555-6232'
 
'721.555.6232'
 
'1.721.555.6232'
 
'17215556232' 
 
); 
   
/* sPhone is the phone number */
 
$sPhone='7085556232'

/* Pattern is all digits */
 
$rPattern='/(\d)/'

/* Replace all non-digits */
 
$rReplace='\D*${1}'

/* Create the regular expression */
 
$sRegExp='/'.preg_replace($rPattern,$rReplace,$sPhone).'/'
   
/* Display it */
 
echo 'Regular Expression: '.$sRegExp."\n"
   
/* Run the test on all the input */
 
foreach ($aPhoneNumbers as $k => $v
         
check($sRegExp,$v); 
  
/* Test function */\
 function 
check($sRegExp,$sPhone
 { 
 echo 
'$sPhone: '.$sPhone.' ? '.preg_match($sRegExp,$sPhone)."\n"
 } 
 
?>
Although I used PHP to develop the regexp logic, I was able to convert it to use the MySQL equivalents - like :digit:, etc.

Output



The .? indicates any single character. You may want to adjust that to [.*]? - meaning any string of characters, or something more specific.

Output doesn't match the code - but - the general idea is to intersperse the target characters with others such that it will match.
In the above code $v would represent the value from the model column in the database table.
So how do you plan to insert the value from the model column into your check() function?

You still haven't showed us how you used it in the SELECT query.
__________________
Jerry Broughton
Reply With Quote
  #16 (permalink)  
Old 11-26-09, 11:14 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Code:
mysql> select "Potterton Puma 80e" REGEXP 'puma[\ \,\-]*80[\ \,\-]*e';
+---------------------------------------------------------+
| "Potterton Puma 80e" REGEXP 'puma[\ \,\-]*80[\ \,\-]*e' |
+---------------------------------------------------------+
|                                                       1 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
In this case, I'd insert something like

Code:
[[:punct:][:print:]]*  between character and number strings.
For example:

PHP Code:

$sRegExp=preg_replace('[\d]*|[a-zA-Z]*','[[:print:][:punct:]]*$(1)',$sString); 

and create the SQL like so:

Code:
SELECT * FROM ... WHERE ... `model` REGEXP '.$sRegExp.' ...
This should effectively place regexps between all strings of letters and numbers so that

Code:
'puma80e' should be converted to 'puma[[:punct:][:print:]]*80[[:punct:][:print:]]*e'
Refer to this page for REGEXP syntax: MySQL :: MySQL 5.1 Reference Manual :: 11.4.2 Regular Expressions

Code:
mysql> select "Potterton Puma 80e" REGEXP 'puma[[:punct:][:print:]]*80[\ \,\-]*e';
+---------------------------------------------------------------------+
| "Potterton Puma 80e" REGEXP 'puma[[:punct:][:print:]]*80[\ \,\-]*e' |
+---------------------------------------------------------------------+
|                                                                   1 |
+---------------------------------------------------------------------+
1 row in set (0.00 sec)

Last edited by wirehopper; 11-26-09 at 11:21 AM.
Reply With Quote
  #17 (permalink)  
Old 11-26-09, 11:39 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
According to wirehopper, this should work pretty good:
PHP Code:

$sRegExp preg_replace('/\W/''[[:punct:][:print:]]*'$entry);
$query "SELECT
    model,
    brand,
    gc_number,
    dir,
    manuals_pdffiles.id,
    pdf_file,
    category,
    pdf_info,
    DATE_FORMAT(date_available, '%d %b %y') AS Fdate,
    safety,
    asbestos,
    class
FROM
    manuals_models
LEFT JOIN
    manuals_pdffiles ON manual_id = manuals_pdffiles.id
LEFT JOIN
    manuals_brands ON brand_id = manuals_brands.id
WHERE
    model REGEXP '
$sRegExp' AND class = 1
ORDER BY
    brand, model "

__________________
Jerry Broughton
Reply With Quote
  #18 (permalink)  
Old 11-27-09, 10:16 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by job0107 View Post
Well, if you think you can come up with a better solution for this particular situation, I would be very interested in seeing it.

Otherwise your criticism is just so much hot air.

Thanks for nothing.
I think it's a fine solution, and like Alane said, it works. What more could anyone want?
I'll take working code over pretty code any day of the week.

Also, FWIW, I don't think carters-site was really criticizing the solution. He may have been a tad inelegant in his response with the word "gross", but I didn't read it as outright criticism. I may be wrong.

Finally, a little late to the party, but I did find this, from Hartmut Holzgraefe:

Regular Expression Functions for MySQL

My Regular Exrepssion UDFs for MySQL have been available on MySQL Forge for a while already, now i've taken the time to create a Trac project page for them. I've implemented the following four functions:

* REGEXP_LIKE(text, pattern [, mode])
* REGEXP_SUBSTR(text, pattern)
* REGEXP_INSTR(text, pattern [,position [,occurence [,return_end [,mode]]]])
* REGEXP_REPLACE(text, pattern, replace)

that behave very similar to their Oracle counterparts, using the same regular expression syntax as the MySQL REGEXP operator.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]

Last edited by End User; 11-27-09 at 10:20 AM.
Reply With Quote
  #19 (permalink)  
Old 12-01-09, 10:38 AM
carters-site's Avatar
carters-site carters-site is offline
Wannabe Coder
 
Join Date: Sep 2009
Location: Moline, IL
Posts: 100
Thanks: 2
Thanked 1 Time in 1 Post
Quote:
Originally Posted by End User View Post
Also, FWIW, I don't think carters-site was really criticizing the solution. He may have been a tad inelegant in his response with the word "gross", but I didn't read it as outright criticism. I may be wrong.
^^^ Yeah my tone was lost a little (happens when you only have text). Just because I use the word "gross" does not mean I was saying I did not respect your solution Job it worked and required some thought so do not take offense.

I chose the term "gross" because I use it a work a lot when I write something that is visually crypitc or (not pretty) I term it as "gross" it does not mean it is not functional or not a good solution to the problem.

I have used plenty of gross solutions to problems in MySQL because lets face it...MySQL forces us treat it like the cheap dirty girl it is. *cough* like creating a 65 column table to handle scaling issues with large stat databases instead of storing a date field like a normal human being.

So Job my apologies if my context came off rougher then I intended. My major was in Computer Science, my english skills have always and will always be terrible.
Reply With Quote
  #20 (permalink)  
Old 12-01-09, 03:19 PM
alane alane is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 71
Thanks: 6
Thanked 0 Times in 0 Posts
Sorry for delay replying but I wanted to use weekent to try out REGEXP suggestion but couldn't get it to work.

Do I just have to replace my original code
PHP Code:

$query "SELECT 

    model, 
    brand, 
    gc_number, 
    dir, 
    manuals_pdffiles.id, 
    pdf_file, 
    category, 
    pdf_info, 
    DATE_FORMAT(date_available, '%d %b %y') AS Fdate, 
    safety, 
    asbestos,
    class 
FROM 
    manuals_models 
LEFT JOIN 
    manuals_pdffiles ON manual_id = manuals_pdffiles.id 
LEFT JOIN 
    manuals_brands ON brand_id = manuals_brands.id 
WHERE 
    (model like '%
$entry%') AND class = 1 
ORDER BY 
    brand, model "

with this or do I have to change anything else?
PHP Code:

$sRegExp preg_replace('/\W/''[[:punct:][:print:]]*'$entry);

$query "SELECT
    model,
    brand,
    gc_number,
    dir,
    manuals_pdffiles.id,
    pdf_file,
    category,
    pdf_info,
    DATE_FORMAT(date_available, '%d %b %y') AS Fdate,
    safety,
    asbestos,
    class
FROM
    manuals_models
LEFT JOIN
    manuals_pdffiles ON manual_id = manuals_pdffiles.id
LEFT JOIN
    manuals_brands ON brand_id = manuals_brands.id
WHERE
    model REGEXP '
$sRegExp' AND class = 1
ORDER BY
    brand, model "

The nested REPLACE version seems to work ok.

Thanks for your help and suggestions.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me on How to Select Records from the database airprince Visual Basic 2 08-16-06 02:06 AM
select variable from one database to use in another?!?? Deansatch PHP 1 07-07-06 02:24 PM
Dynamic select box from database values PrashantJsp JavaScript 2 05-04-05 12:29 AM
PHP Can't select database EST PHP 4 08-19-03 10:14 AM


All times are GMT -5. The time now is 05:09 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.