对不起,如果这是一个愚蠢的问题,但最近没有使用太多的SQL,并且无法找到很多帮助。MySQL使用WHERE子句显示所有列
我需要获取所有行以显示结果,即使结果为0或null。我遇到的问题是由于WHERE子句(没有WHERE行都显示,但数据不是)。
SELECT SUM(c.amount) AS 'total_income', p.ref_id AS 'property'
FROM property p
LEFT JOIN contract c
ON p.id = c.property_ref_id
LEFT JOIN contract_payment cp
ON c.id = cp.contract_id
WHERE cp.paid = 1 AND year(cp.date_paid) = :year
GROUP BY p.id
显示结果集,而不在哪里,错误数据会是这样
array
0 =>
array
'total_income' => null
'property' => string 'test/0001' (length=9)
1 =>
array
'total_income' => null
'property' => string 'test/0002' (length=9)
2 =>
array
'total_income' => string '200' (length=3)
'property' => string 'test/0003' (length=9)
3 =>
array
'total_income' => string '16100' (length=5)
'property' => string 'test/0004' (length=9)
虽然这是结果与WHERE子句和良好的数据集,但不是所有的行
array
0 =>
array
'total_income' => string '4200' (length=4)
'property' => string 'test/0004' (length=9)
是否有人请赐教我可以对SQL进行哪些修改以便检索我想要的数据?
我想你的意思行,而不是列。 – Barmar 2013-04-04 09:36:09
你是对的Barmar,编辑的问题。 – 2013-04-04 09:38:38