Oracle SQL INNER Join based on non-matching values

戏子无情 提交于 2019-12-11 11:46:53

问题


ColA(Table1)     ColB(Table2)
AB3               AB_MNO_3
AB3               AB_PQR_3
AB4               AB_MNO_4
AB4               AB_PQR_4

I want to do Inner Join based on columns in two table with some of the non-equal values shown above. So, Table1 can have AB3 which should be matched against AB_MNO_3, AB_PQR_3 while AB4 should be matched against AB_MNO_4, AB_PQR_4 Rest of the values in these columns in two tables do match. Would highly appreciate if anyone provides recommendations around the same.


回答1:


SELECT * FROM Table1 tl INNER JOIN Table2 t2 
ON (tl.ColA = t2.ColB OR (tl.ColA ='AB3' AND t2.ColB='AB_MNO_3')
OR (tl.ColA ='AB3' AND t2.ColB='AB_PQR_3') OR (tl.ColA ='AB4' AND t2.ColB='AB_MNO_4')
OR (tl.ColA ='AB4' AND t2.ColB='AB_PQR_4'))



回答2:


From your examples : I assume that first two chars of colA and Colb equal AND last char of colA and ColB equal

SELECT * FROM TABLE1 t1 INNER JOIN
TABLE2 t2 ON 
substr(t1.ColA,0,2) =  substr(t2.ColB,0,2) 
AND
substr(t1.cola,length(t1.cola),1) = substr(t2.colb,length(t2.colb),1)


来源:https://stackoverflow.com/questions/7970957/oracle-sql-inner-join-based-on-non-matching-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!