问题
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