我無法從我的查詢中獲得結果。 我想獲取交易中單位銷售的總額和總數,交易位於特定的zip中。SQL LEFT JOIN無法正常工作
這裏是我的表:
TABLE unit_type(
id (Primary key)
unit_name (varchar)
department_id (Foreign key)
)
TABLE transaction(
id (PK)
commission_fix_out (int)
transaction_end_week (int)
property_id (FK)
unit_type_id (FK)
...
)
TABLE property(
id (PK)
property_zip_id (FK)
...
)
我unit_types表中有以下記錄:
+-----+----------------------+----------------+
| id | unit_name | department_id |
+-----+----------------------+----------------+
| 1 | WV construction | 1 |
| 2 | WV resale | 1 |
| 3 | WV rent | 1 |
| 4 | BV industrial sale | 2 |
| 5 | BV industrial rent | 2 |
| ... | ... | ... |
+-----+----------------------+----------------+
這裏是我的查詢看起來像:
SELECT SUM(commission_fix_out), COUNT(commission_fix_out), unit_name, ut.id
FROM unit_type as ut
LEFT JOIN transaction as t
ON ut.id = t.unit_type_id
RIGHT JOIN property as p
ON (p.id = t.property_id AND p.property_zip_id = 1459)
WHERE ut.department_id = 1
GROUP BY unit_name
ORDER BY ut.id
導致:
+------------+-------------+-------------+---------+
| SUM(...) | COUNT(..) | unit_name | ut.id |
+------------+-------------+-------------+---------+
| 40014 | 11 | WV resale | 2 |
| NULL | 0 | WV rent | 3 |
+------------+-------------+-------------+---------+
我期待着WV建設,但它不顯示。任何人知道我在哪裏錯了?
檢查此http://phpweby.com/tutorials/mysql/32,它可能有助於左/右加入 – bpgergo 2012-08-01 12:14:51