I have a query
SELECT
users.email AS email,
addons.sku AS sku,
addons.quantity as quantity,
invoices.total as total
FROM addons
INNER JOIN users ON 1=1
and users.id = addons.user_id
LEFT JOIN invoices ON 1=1
AND invoices.user_id = users.id
AND invoices.status != 3
Here is what I need to happen:
- if user doesn't have an invoice at all we should include them with
NULL
being returned in thetotal
- if user has an invoice in status
!= 3
we should include them - if invoices exists and status = 3 we should exclude them.
So it's like I need both INNER JOIN
and LEFT JOIN
at the same time
How can I achieve that?