下面的MYSQL查询结合了许多表。但是,如您所见,我想在收据表的末尾添加一个LEFT JOIN。当我添加LEFT JOIN时,查询返回错误。任何人都知道将收据表保留到查询其余部分的最佳方法。抱歉,这是一个新手问题。谢谢 !!

SELECT user_name, expense_category, merchant_name, expense_cost, expense_date, expense_status,    receipt_image, expense_comment
FROM users, expenses, merchants, receipts
WHERE ".$adminId." = expenses.admin_id
AND expenses.user_id = users.user_id
AND expenses.merchant_id = merchants.merchant_id
AND LEFT JOIN (receipts)
ON expenses.receipt_id = receipts.receipt_id

最佳答案

这是一种干净的方法,请注意,我为表添加了别名以提高可读性,因此您可以在select语句中使用别名来从适当的表中获取列。

SELECT
u.user_name,
ex.expense_category,
mer.merchant_name,
ex.expense_cost,
ex.expense_date,
ex.expense_status,
re.receipt_image,
ex.expense_comment
FROM users u
JOIN expenses ex on ex.user_id = u.user_id
JOIN merchants mer on mer.merchant_id = ex.merchant_id
LEFT JOIN receipts re on re.receipt_id = ex.receipt_id
where
ex.admin_id = '$adminId'

09-28 04:21