如果我对您的理解正确,我认为您追求的是:
WITH your_table AS (SELECT 1 user_id, '210|9|211' user_country FROM dual UNION ALL
SELECT 2 user_id, '210|211' user_country FROM dual UNION ALL
SELECT 3 user_id, '9|87' user_country FROM dual UNION ALL
SELECT 4 user_id, '210|117|54' user_country FROM dual),
-- end of mimicking a table containing your data; you wouldn't need the above subquery
-- as you would just select directly from your table instead
vals_to_check AS (SELECT regexp_substr(:p_values_to_check_for, '[^'||:p_delimiter||']+', 1, LEVEL) val,
COUNT(DISTINCT regexp_substr(:p_values_to_check_for, '[^'||:p_delimiter||']+', 1, LEVEL)) OVER () cnt_vals
FROM dual
CONNECT BY regexp_substr(:p_values_to_check_for, '[^'||:p_delimiter||']+', 1, LEVEL) IS NOT NULL)
SELECT yt.user_id,
yt.user_country
FROM your_table yt
INNER JOIN vals_to_check vtc ON :p_delimiter||yt.user_country||:p_delimiter LIKE '%'||:p_delimiter||vtc.val||:p_delimiter||'%'
GROUP BY yt.user_id,
yt.user_country,
vtc.cnt_vals
HAVING COUNT(*) = cnt_vals;
结果:
-- with the bind variables set to the following:
variable p_delimiter varchar2
variable p_values_to_check_for varchar2
exec :p_delimiter := '|'; :p_values_to_check_for := '210|9';
USER_ID USER_COUNTRY
---------- ------------
1 210|9|211
-- with the bind variables set to the following:
variable p_delimiter varchar2
variable p_values_to_check_for varchar2
exec :p_delimiter := '|'; :p_values_to_check_for := '210|211';
USER_ID USER_COUNTRY
---------- ------------
1 210|9|211
2 210|211
注意我直接在 sql 中对输入字符串进行了拆分——不需要单独的 pl/sql 函数调用。但是,您可能希望继续使用您的 split_text_fn 函数,在这种情况下,您可以跳过声明vals_to_check
子查询而只table(split_text_fn(...))
在主 SQL 语句中使用您的。