Can I use the operators(AND & OR) like this in a LEFT JOIN after ON?
Yes, you can use AND and OR operators within your JOIN clause.
Is this query correct and most efficient way of achieving this?
mysql.
The below conditions are going to cause you performance issues with your query.
lower(t2.creditor_name) LIKE '%vehicle finance%'
OR lower(t2.creditor_name) LIKE '%vehicle and asset finance%'
- Anytime you wrap a function around a column, such as
lower(t2.creditor_name), you are forcing the query engine to convert that column to lowercase for every row in the table, before it can compare it to something.
- Putting a leading
% in a LIKE condition is the equivalent of looking for '%oe, John' in the phonebook instead of 'Doe, Joh%'. Without knowing the first letter is D, someone would have to scan every page of the phonebook to find a record that could be a possible match.
Using these anti-patterns is generally a bad idea, and most often will make it very difficult for the query to utilize an index. You might not notice the impact initially, if your table only has a few hundred rows in it. However, as the data set grows, and you start getting into the 100s of thousands or millions of rows, it will most certainly matter.