2014-11-25 62 views
0

我想要做这样的事情 REPLACE()几个值,并使用的结果()

SELECT 
    id, 
    num 
FROM 
    sometable 
WHERE 
    num IN (REPLACE ('K-123, K-456, K-678', 'K-', '')); 

但:

(REPLACE ('K-123, K-456, K-678', 'K-', '')) 

回报:“ 123,456,678'

没有如我所料:“123”,“456”,“678”

所以,是有可能使元素列表REPLACE()使用该结果作为论据()后?

谢谢。

回答

1

您可以使用find_in_set()

where find_in_set(num, replace(replace('K-123, K-456, K-678', 'K-', ''), ', ', ',')) > 0 
+0

如果我理解正确FIND_IN_SET()如果找到匹配这个函数必须返回位置。 'code' SELECT FIND_IN_SET(123,REPLACE(REPLACE('K-123,K-456,K-678','K-',''),',','')) nothing returned 'code 'SELECT REPLACE(REPLACE('K-123,K-456,K-678','K-',''),',','') return 123456678它是正确的吗? – Initialis 2014-11-25 13:23:04

+0

糟糕,目的是用'',''替换'',''。 – 2014-11-25 13:33:34

+0

'code'SELECT FIND_IN_SET(123,REPLACE(REPLACE('K-123,K-456,K-678','K-',''),',',',')) return nothing – Initialis 2014-11-25 13:38:18