4

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

4

5 回答 5

1

我设法解决了我的问题。我想和你分享我的结果:

SELECT SUM(commission_fix_out), COUNT(commission_fix_out), unit_name
FROM unit_type ut
LEFT JOIN transaction t
ON (ut.id = t.unit_type_id AND t.property_id IN (SELECT id FROM property p WHERE
property_zip_id = 1459))   
WHERE department_id = 1
GROUP BY unit_name
ORDER BY ut.id

我没有使用额外的 JOIN,而是尝试在我的 ON 子句中使用子查询,这给出了我的下一个结果:

+-----------+-----------+-------------------+------+
| SUM(..)   | COUNT()   | unit_name         | id   |
+-----------+-----------+-------------------+------+
| NULL      | 0         | WV construction   | 1    |
| 40014     | 11        | WV resale         | 2    |
| NULL      | 0         | WV rent           | 3    |
+-----------+-----------+-------------------+------+

我要感谢所有帮助我解决这个问题的人。

于 2012-08-02T09:13:54.690 回答
0

我认为这是导致问题的正确加入。

试试这个 :

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
WHERE ut.department_id = 1
GROUP BY unit_name
ORDER BY ut.id

结果是什么 ?

于 2012-08-01T12:11:48.207 回答
0

这可能无法解决问题,但为什么RIGHT JOIN property呢?

LEFT JOIN property相反会更有意义。

你看,transaction表已经被LEFT JOIN编辑到unit_type我假设是这个查询的基表。

于 2012-08-01T12:12:53.920 回答
0

右连接将有效地撤消它之前的左连接点

于 2012-08-01T12:15:37.813 回答
0

试试这个:

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
             LEFT JOIN `property` p
                 ON p.id = t.property_id
WHERE     ut.department_id = 1 AND
          p.property_zip_id = 1459
GROUP BY  unit_name, p.property_zip_id -- added another column
ORDER BY  ut.id

更新 1

SELECT *
FROM
    (
        (
            SELECT    SUM(commission_fix_out) total_fix_out, 
                      COUNT(commission_fix_out) count_fix_out, 
                      unit_name, 
                      ut.id
            FROM      unit_type as ut
                         LEFT JOIN `transaction` as t
                             ON ut.id = t.unit_type_id
                         LEFT JOIN `property` p
                             ON p.id = t.property_id
            WHERE     ut.department_id = 1 AND
                      p.property_zip_id = 1459
            GROUP BY  unit_name, p.property_zip_id -- added another column
            ORDER BY  ut.id
        ) tableA
        UNION
        (
            SELECT  0 as total_fix_out,
                    0 as count_fix_out,
                    unit_name,
                    id
            FROM    unit_type
            WHERE   id NOT IN
                    (
                        SELECT  DISTINCT xx.id
                        FROM    unit_type as xx
                                     LEFT JOIN `transaction` as t
                                         ON xx.id = t.unit_type_id
                                     LEFT JOIN `property` p
                                         ON p.id = t.property_id
                        WHERE     xx.department_id = 1 AND
                                  p.property_zip_id = 1459
                    )
        ) tableA
    ) tableC
于 2012-08-01T12:26:24.117 回答