我有一个查询
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
这是我需要做的:
NULL
中将total
退还给他们!= 3
,则应将其包括在内所以就像我同时需要INNER JOIN
和LEFT JOIN
我该如何实现?
答案 0 :(得分:1)
这就是您需要的:
SELECT
users.email AS email,
addons.sku AS sku,
addons.quantity as quantity,
invoices.total as total
FROM addons
INNER JOIN users
ON users.id = addons.user_id
LEFT JOIN invoices
ON invoices.user_id = users.id
WHERE invoices.status IS NULL OR invoices.status != 3
说明:
LEFT JOIN
的目的status
的原因,NULL
可能为LEFT JOIN
)WHERE
子句