如何使用like函数(oraclesql)从另一个表中查找代码列表?

rekjcdws  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(505)

我有一个表调用'student\u table'表,它有超过100个std\u id,我需要从'reg\u table'表Mapstudent\u regu id
学生表格格式

STD_ID
123
456
789
688

reg\U表格格式第1列:学生reg\U 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

如何获得如上所示的Map输出?
我不想给超过100个std\U id的类似函数如下

STD_ID Like  '%123% or '%456% or .......

如何在一个sql中获得它?

zysjyyx4

zysjyyx41#

你使用的方法是对的 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|$)');

相关问题