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

前端 未结 2 1067
温柔的废话
温柔的废话 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:36

    You can't do this directly using a variable. Based on what you have now, the server is trying to execute select * form t where c in ('1,2,3'); ... which clearly isn't what you want given that you're checking to see if c contains the single string given.

    You must generate dynamic SQL based off of your list in order to get this to work.

提交回复
热议问题