WITH t AS ( SELECT '1' val FROM DUAL UNION ALL SELECT '1,2' val FROM DUAL UNION ALL SELECT '1,2,3' val FROM DUAL UNION ALL SELECT '1,2,3,4' val FROM DUAL UNION ALL SELECT '2' val FROM DUAL UNION ALL SELECT '2,3' val FROM DUAL UNION ALL SELECT '2,3,4' val FROM DUAL UNION ALL SELECT '3' val FROM DUAL UNION ALL SELECT '3,4' val FROM DUAL UNION ALL SELECT '4' val FROM DUAL ) SELECT * FROM T ; 전체 데이터에서 조회 조건을 '1,2' 로 하게 될 경우 1 또는 2가 포함된 모든 데이터를 출력하려고 합니다. 예를 들어, 결과값은 '1' '1,2' '1,2,3' '1,2,3,4' '2' '2,3' '2,3,4' 여러가지 방법으로 IN 또는 LIKE 를 걸어도 원하는 결과가 나오지 않네요... 질문을 하게 된 계기는 멀티 체크 박스에 다중 체크 후 조회를 하게 될 경우 체크 한 모든 값 중 하나라도 포함 될 때 결과를 받기 위함입니다. 감사합니다.
WITH t AS ( SELECT '1' val FROM dual UNION ALL SELECT '1,2' FROM dual UNION ALL SELECT '1,2,3' FROM dual UNION ALL SELECT '1,2,3,4'FROM dual UNION ALL SELECT '2' FROM dual UNION ALL SELECT '2,3' FROM dual UNION ALL SELECT '2,3,4' FROM dual UNION ALL SELECT '3' FROM dual UNION ALL SELECT '3,4' FROM dual UNION ALL SELECT '4' FROM dual UNION ALL SELECT '10' FROM dual ) -- 1자리 고정 자리수 라면? SELECT * FROM t WHERE REGEXP_LIKE(val, '1|2') ; -- 가변 자리수 라면? SELECT * FROM t WHERE REGEXP_LIKE(','||val||',', ',1,|,2,') ;
WITH t AS ( SELECT '1' val FROM DUAL UNION ALL SELECT '1,2' val FROM DUAL UNION ALL SELECT '1,2,3' val FROM DUAL UNION ALL SELECT '1,2,3,4' val FROM DUAL UNION ALL SELECT '2' val FROM DUAL UNION ALL SELECT '2,3' val FROM DUAL UNION ALL SELECT '2,3,4' val FROM DUAL UNION ALL SELECT '3' val FROM DUAL UNION ALL SELECT '3,4' val FROM DUAL UNION ALL SELECT '4' val FROM DUAL ) SELECT VAL FROM T WHERE LENGTH(VAL) - NVL(LENGTH(REPLACE(REPLACE(VAL,'1',''),'2','')),0) > 0 -------------------------------------------------------------------------- SELECT * FROM T WHERE LENGTH(VAL) - NVL(LENGTH(REGEXP_REPLACE(VAL,'1|2','')),0) > 0 ;