我无法从查询中获取结果。我想在交易中获取单位销售的总和和总数,其中交易在特定的 zip 中。
这是我的表:
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 结构,但它没有出现。有谁知道我在哪里错了吗?