如果我理解正確的話,我覺得你以後是這樣的:
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
子查詢,並在主SQL語句中使用table(split_text_fn(...))
。
請發佈您嘗試的代碼。 –
你會考慮正常化你的db模型嗎?同一列中的多個數據不是一種好的做法。 –
另外,請不要所需結果爲該數據 – Aleksej