If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > RegExp Pattern for a Tag Column

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-05-07, 06:43
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
RegExp Pattern for a Tag Column

Hi

I have a column named Tags of varchar type which hold tags separated by commas.

Example of a possible field value for Tags: bikes, road trip, summer

Code:
SELECT * FROM `tablename` WHERE `Tags` LIKE '%road%'
will obviously select the example row but it shouldn't since it doesn't come under road but under road trip.

Using PHP's preg_match is easy but in this case I need a SQL solution.

How do I go abt making the condition for the query using the Regexp (^|,\s*)road(\s*,|$) ?

Now I got this to work and it doesnt select the road trip row for road, but neither does it get selected for road trip
Code:
(^|,\s*)road trip(\s*,|$)
Any idea what I might be dong wrong here ?

Thanx
__________________
MySQL 5.1
Reply With Quote
  #2 (permalink)  
Old 02-05-07, 06:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
make sure there are no extraneous spaces around your commas, and then do this:

... WHERE CONCAT(',',Tags,',') LIKE '%,road,%'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-05-07, 07:01
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Nice workaround, but having to change the current set of data to get trim whitespaces surrounding the commas is not viable.

Anyway, MySQL doesnt seem to recognize \s - I just replaced it with a whitespace and it worked !

Code:
(^|, *)road trip( *,|$)
__________________
MySQL 5.1
Reply With Quote
  #4 (permalink)  
Old 02-05-07, 08:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
well, if every comma is consistently followed by a space, then you can simply do it like this --
Code:
... WHERE CONCAT(', ',Tags,', ') LIKE '%, road, %'
see the difference?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-05-07, 09:14
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Problem is, the tag information was entered by users via an html form (were told to separate tags by a comma) which is still online and I just can't rely on the data entered by them. Obviously, the back-end script would be modified to strip spaces surrounding commas, but for the existing data, I'll need to assume any number of spaces surrounding a comma.
__________________
MySQL 5.1
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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