有问题要问。当我使用左连接时,即使某个行的空值为空,我如何列出我的左表以显示数据?
继承我的疑问:
SELECT *, SUM(transactions.debit_credit) current_spending
FROM (`budget`) LEFT JOIN `categories` ON `budget`.`category`=`categories`.`idcategory`
LEFT JOIN `user_category` ON `budget`.`category`=`user_category`.`idcategory`
LEFT JOIN `category_transaction` ON `budget`.`category`=`category_transaction`.`idcategory`
LEFT JOIN `transactions` ON `category_transaction`.`idtransaction`=`transactions`.`idtransaction`
WHERE `user_category`.`iduser` = '1'
AND MONTH(transactions.transaction_date) = '11'
GROUP BY `budget`.`idbudget`;
当我删除AND MONTH(transactions.transaction_date) = '11'
时,查询正常接受它会显示所有数据而不是仅仅1个月的交易。
帮助??请??任何想法怎么??
答案 0 :(得分:0)
您的where
子句会将您的left join
变为inner join
,因为您会根据已连接的表格过滤数据。
而是将该条件放在您的联接的on
子句中
SELECT *, SUM(transactions.debit_credit) current_spending
FROM (`budget`)
LEFT JOIN `categories` ON `budget`.`category`=`categories`.`idcategory`
LEFT JOIN `user_category` ON `budget`.`category`=`user_category`.`idcategory`
AND `user_category`.`iduser` = '1'
LEFT JOIN `category_transaction` ON `budget`.`category`=`category_transaction`.`idcategory`
LEFT JOIN `transactions` ON `category_transaction`.`idtransaction`=`transactions`.`idtransaction`
AND MONTH(transactions.transaction_date) = '11'
GROUP BY `budget`.`idbudget`;