I have two strings in comma separated. String1='ABC,DEF,PQR,MNO,XYZ' String2='PQR,FGH' String3='GHI,JKL,TUV' Now I want that, compare string1 and string2, it will return true because String2 has value 'PQR' that matches with String1. But If I compare String1 and String3, it will return false because none of the string from string 3 match with String1. Note : Both Strings I got using Group_concat function.
Asked
Active
Viewed 204 times
-1
-
3[Topic is already discussed here](http://stackoverflow.com/questions/15280956/how-to-compare-two-comma-separated-strings-and-return-true-if-there-is-at-least) and [here as well](http://stackoverflow.com/questions/15693316/compare-comma-delimited-strings-in-sql) – mitkosoft Feb 12 '16 at 12:35
-
This type of question is asked again and again on stack overflow. The first answer most people give is (shouting intended): NORMALIZE YOUR DATA CORRECTLY. Unless you have a very good reason otherwise, it is a terrible idea to store data in a database as comma separated strings. The items between the commas should be in a separate table with a one-to-many or many-to-many relation with the master table. – rghome Feb 12 '16 at 13:21
-
It is possible, but maybe avoidable. Can you put up your actual query(s) that get this data to see if there is a way to avoid needing to do this? If not you could write a MySQL function to do it (unfortunately the suggestions by @mitkosoft are for MsSql rather than MySQL), or do it with some trickery in an SQL statement, but how to do it depends on how you get the data. If you have already retrieved it from you database then probably best to just do it in your calling script – Kickstart Feb 12 '16 at 14:36