mysql passing data to in

别说谁变了你拦得住时间么 提交于 2020-01-14 04:29:07

问题


Basically I want to fetch some data from tblvw1. Another table contains possible ids within a column which is stored as concatenated string, for example: "1|2|3|4". Next I have tried to get the result by the following query:

SELECT x FROM tblname1 
WHERE id IN (SELECT REPLACE(content,'|',',') FROM tblname2 WHERE dataid = y )

AS result I only get the first value, the other data wasn't fetched. I suppose that the subquery will result in this form:

SELECT x FROM tblname1 WHERE id IN ('1,2,3,4')  

but i want to have this subquery either as

 SELECT x FROM tblname1 WHERE id IN (1,2,3,4)  

or

  SELECT x FROM tblname1 WHERE id IN ('1','2','3','4')  

Any Idea?


回答1:


Can you use the find_in_set function?

SELECT x FROM tblname1 t1
inner join tblname2 t2 on find_in_set (t1.id, REPLACE(t2.content,'|',',')) > 0
where t2.dataid = 'y';

The find_in_set function returns the position of the first argument within the second argument. If the result is >0, then the first argument has been found.

See http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set



来源:https://stackoverflow.com/questions/41061181/mysql-passing-data-to-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!