簡體   English   中英

如何使用 function (Oracle sql )從另一個表中查找代碼列表?

[英]How to find for the list of code from another table using like function (Oracle sql )?

我有表調用“student_table”表,它有 100 多個 STD_ID,我需要從“Reg_table”表中 map Student_reg_ID

學生表格式

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

需要 Output

STD_ID    Student_reg_ID 
123       23124
456       56142
789       14328
688       14328

我怎樣才能得到 map output 如上所示?

不想在 function 中給出超過 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|$)');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM