mysqlwhere-clausefind-in-set

FIND_IN_SET on columns? Does it make sense?


I stumbled across a strange expression, which doesn't make sense to me at all.

SELECT * FROM `table` WHERE FIND_IN_SET('string', `column`);

Why not:

SELECT * FROM `table` WHERE `column`='string';

I thought FIND_IN_SET is used to find strings in comma-separated string lists. Are there cases where I can benefit from a "FIND_IN_SET"-column expression instead of a simple WHERE?


Solution

  • FIND_IN_SET is typically used the other way around: to find a value in a comma-separated list passed as a parameter. Storing values as comma-separated lists in a database is generally not recommended.:

    select * 
    FROM `table` 
    WHERE FIND_IN_SET(`column`, '1,2,3,4,5');