Use a comma-separated string in an `IN ()` in MySQL

前端 未结 2 1069
温柔的废话
温柔的废话 2021-01-20 23:35

If a user-defined variable @x is a string of comma separated numbers, e.g. \'1,2,4\', is there a way to use it in an IN () function?

2条回答
  •  孤街浪徒
    2021-01-21 00:31

    Maybe there is no point of answering this question now, but recently I faced same problem and this is how I resolved it.

    It worked for me, hope this is what you were looking for.

    select * from table_name where CONCAT(',',('1,2,4'),',') LIKE CONCAT('%,',column_name,',%');
    

    Example: It will look like this

    select * from t where ',1,2,4,' LIKE '%,2,%';
    

提交回复
热议问题