I have two database tables: orders and customers.
I'm running a SQL to get all orders in June month.
If the Ship To and Bill To email are different, we are inserting two different records with both emails to customers table.
select o.order_id
, o.total
, c.customer_email
from orders o
left
join customers c
ON o.bill_email = c.customer_email
OR o.ship_email = c.customer_email
where DATE(o.order_date) >= '2020-06-01'
But this SQL is taking too much time to load because of the condition,
ON o.bill_email=c.customer_email
OR o.ship_email=c.customer_email
How can I add both conditions in ON clause?
Any help would be appreciated.
Use two
left joins and put the results in separate columns rather than rows:Note that the
date()function is not needed.That said, this seems more easily expressed as: