如何使用类似函数(Oracle sql)从另一个 table 中查找代码列表?
How to find for the list of code from another table using like function (Oracle sql )?
我有 table 调用 'student_table' table 有超过 100 个 STD_ID,
我需要从 'Reg_table' table
映射 Student_reg_ID
student_table格式
STD_ID
123
456
789
688
Reg_table格式
第 1 列:Student_reg_ID
第 2 列:参考资料
Student_reg_ID Reference
23124 stden id 123
56142 customer refer 456
14328 refer - 789
67890 code ref : 688
需要输出
STD_ID Student_reg_ID
123 23124
456 56142
789 14328
688 14328
如何获得如上所示的输出映射?
我不想给出超过100个STD_ID的类似函数如下
STD_ID Like '%123% or '%456% or .......
我怎样才能一次得到它 sql?
您在这里使用 LIKE
是正确的。试试这个选项:
SELECT
st.STD_ID,
rt.Student_reg_ID
FROM student_table st
LEFT JOIN Reg_table rt
ON rt.Reference LIKE '%' || st.STD_ID || '%';
请注意,如果 STD_ID
列是一个数字,那么在进行 LIKE
比较之前,您首先必须转换为文本,即使用此:
LEFT JOIN Reg_table rt
ON rt.Reference LIKE '%' || TO_CHAR(st.STD_ID) || '%';
编辑:
考虑到 STD_ID
值可能并不总是三位数,我们可以使用 REGEXP_LIKE
在加入时强制执行完全匹配:
SELECT
st.STD_ID,
rt.Student_reg_ID
FROM student_table st
LEFT JOIN Reg_table rt
ON REGEXP_LIKE(rt.Reference, '(^|\s)' || st.STD_ID || '(\s|$)');
我有 table 调用 'student_table' table 有超过 100 个 STD_ID, 我需要从 'Reg_table' table
映射 Student_reg_IDstudent_table格式
STD_ID
123
456
789
688
Reg_table格式 第 1 列:Student_reg_ID 第 2 列:参考资料
Student_reg_ID Reference
23124 stden id 123
56142 customer refer 456
14328 refer - 789
67890 code ref : 688
需要输出
STD_ID Student_reg_ID
123 23124
456 56142
789 14328
688 14328
如何获得如上所示的输出映射?
我不想给出超过100个STD_ID的类似函数如下
STD_ID Like '%123% or '%456% or .......
我怎样才能一次得到它 sql?
您在这里使用 LIKE
是正确的。试试这个选项:
SELECT
st.STD_ID,
rt.Student_reg_ID
FROM student_table st
LEFT JOIN Reg_table rt
ON rt.Reference LIKE '%' || st.STD_ID || '%';
请注意,如果 STD_ID
列是一个数字,那么在进行 LIKE
比较之前,您首先必须转换为文本,即使用此:
LEFT JOIN Reg_table rt
ON rt.Reference LIKE '%' || TO_CHAR(st.STD_ID) || '%';
编辑:
考虑到 STD_ID
值可能并不总是三位数,我们可以使用 REGEXP_LIKE
在加入时强制执行完全匹配:
SELECT
st.STD_ID,
rt.Student_reg_ID
FROM student_table st
LEFT JOIN Reg_table rt
ON REGEXP_LIKE(rt.Reference, '(^|\s)' || st.STD_ID || '(\s|$)');