简体   繁体   中英

How to use inner join for 3 columns with condition MYSQL query

I have 2 tables in table sample1 have key1 and key2.... in table sample2 have tey1 and tey2 and tey3....

i want join tables with this condition 1_ if key1 = tey1 Or 2_ if key2 = tey2 or key2 = tey3

and if key1 = tey1 dontcheck key2 = tey2 or key2 = tey3

my querie is:

Select *
from sample1 inner join sample2 
on  sample1.key1 = sample2.tey1 OR  sample1.key2 = sample2.tey2 OR sample1.key2 = sample2.tey3    

but it's too slow and i think not optimal

select s1. ,s2. from sample1 as s1, sample2 as s2 where s1.key1 = s2.tey1 OR s1.key2 = s2.tey2 OR s1.key2 = s2.tey3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM