I'm managing a DB and I'd like to know if there is a cooler way to make this query.
My actual query:
SELECT * FROM mytable
LEFT JOIN table1 AS m1 ON mytable.idA=m1.K
LEFT JOIN table1 AS m2 ON mytable.idB=m2.K
LEFT JOIN table1 AS m3 ON mytable.idC=m3.K
LEFT JOIN table1 AS m4 ON mytable.idD=m4.K
LEFT OUTER JOIN table2 AS t1 ON mytable.idK1= t1.idK1 AND mytable.idK2= t1.idK2
LEFT OUTER JOIN table3 ON t1.idT = table3.idT
WHERE m1.K = "my_value" OR m2.K = "my_value" OR m3.K = "my_value" OR m4.K = "my_value"
I have a query that make 4 joins on the same other table because I can have the value in the field idA, idB, idC or idD. After that I make other 2 joins with other tables.
My question is:
Can I improve this code? Is there a smarter way to do it or a better logic to follow?
Thanks a lot, even for the theoretical explanations.