2012-08-01 58 views
4

我無法從我的查詢中獲得結果。 我想獲取交易中單位銷售的總額和總數,交易位於特定的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建設,但它不顯示。任何人知道我在哪裏錯了?

+0

檢查此http://phpweby.com/tutorials/mysql/32,它可能有助於左/右加入 – bpgergo 2012-08-01 12:14:51

回答

1

我設法解決我的問題。我想與大家分享我的結果:

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

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 

而不是使用一個額外的加入,我會用我的ON子句的子查詢這給我的下一個嘗試的結果我想感謝大家幫助我解決這個問題。

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 

的結果是什麼?

+0

我想這不會導致問題。如果我通過(左)連接切換正確的連接,它給了我相同的結果。 – 2012-08-01 12:21:04

+0

我更新了我的答案。 :) – 2012-08-01 12:31:51

+0

與我評論的其他答案相同,我獲得了所有單位。但是,現在它給出了交易的結果。雖然我只需要給定zip的結果。 – 2012-08-01 13:18:09

0

這可能沒有解決問題,但爲什麼RIGHT JOIN property

LEFT JOIN property反而會更有意義。

你看,transaction表已經LEFT JOIN編輯unit_type我認爲這是這個查詢的基表。

0

這項權利加入將有效地撤消LEFT點JOIN之前

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 
+0

有點太快接受這個答案,我的apoligies。 雖然我現在得到3個單位,它不會給我每個zip的結果。該查詢現在返回所有事務的總和。正如你可以看到在下面的出口: [鏈接](https://docs.google.com/spreadsheet/ccc?key=0AlEr3UxU5p5GdFZPWUJPQk5VelZXTnFweWJIQmEtZ0E) – 2012-08-01 13:13:57

+0

試試我的更新答案。 – 2012-08-01 13:18:33

+0

我試過了,它給我返回了和我原來的帖子 – 2012-08-01 13:44:13