D&C Lug - Home Page
Devon & Cornwall Linux Users' Group

[ Date Index ][ Thread Index ]
[ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] comma separated value string comparisons



On 10 Jul 2003 at 0:14, Neil Williams wrote:

> What's the best way to compare two strings, each containing comma
> separated values, to see if ANY of the values in the first string
> match any of the values in the second string?
> 
> Ideally, I'd like to do this within an existing SQL query, but PHP
> could be done (and therefore Perl as PHP can use perl regular
> expression pattern matching).
> 

You don't say what database you are using. But the following works for MySQL.
Otherwise it would translate to PERL or PHP easily (and wouldn't look such a hack!).
The SQL looks horrible and i can't comment on the efficiency.

> e.g.
> $string1 = "PHP, XML, GnuPG, standards, design";
> $string2 = "C++, Perl, PHP, networking, security";
> 
> So $string1 should give a match with $string2.
> 

SELECT "PHP, XML, GnuPG, standards, design"
RLIKE REPLACE ( 
 REPLACE ( 
  REPLACE ( 'C++,Perl ,PHP ,networking,security','+','[+]'),
 ' ',''),
',','|') 


Cheers,

Pete.
> -- 
> 
> Neil Williams
> =============
> http://www.codehelp.co.uk
> http://www.dclug.org.uk
> 
> http://www.wewantbroadband.co.uk/
> 
> 



--
The Mailing List for the Devon & Cornwall LUG
Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the
message body to unsubscribe.


Lynx friendly