2016-05-17 58 views
0

所以我想我得太多又和這似乎不能歸結爲一個解決方案時返回表中的所有記錄。另外,我認爲我的自動增量不能在數據庫中完全工作。任何人,我試圖加入2個表格,並使用「修剪」列來匹配另一列,並檢索2列...很抱歉,如果這是非常混亂。這裏是我的嘗試:如何加入表已經丟失記錄

PS ...它的工作原理或多或少,但是當條件爲空,它不返回任何東西....我怎麼做,以便它仍然回來?在另一個表中仍然滿足條件,即workd_id是11 ...但我必須單獨手動添加它。思考?謝謝!

INSERT INTO permits.JOB_CATEGORY(jc_desc, WORK_TYPE_workd_id) 
    SELECT DISTINCT 
     JOB_CATEGORY, workd_id 
    FROM edmonton_upto_10may2016_mostly_text 
    JOIN WORK_TYPE 
    ON WORK_TYPE = CONCAT('(',work_num,')',' ',work_desc) 
    GROUP BY JOB_CATEGORY; 

下面是從表WORK_TYPE數據

workd_id workd_desc     work_num 
    1  New        01 
    2  Interior Alterations   03 
    3  Demolition      99 
    4  Exterior Alterations   03 
    5  Deck Attached     03 
    6  Building - New     01 
    7  Add Suites to Single Dwelling 07 
    8  Addition      02 
    9  Foundation      04 
    10 Hot Tub       14 
    11  
    12 Structure      05 
    13 Move Building on to Site  12 
    14 Move Building OnSite   12 
    15 Add Suites to Multi-Dwelling 08 
    16 Convert Non-Res to Residential 09 
    17 Swimming Pool     14 
    18 Remove Suites     11 
    19 Convert Residential to Non-Res 10 
    20 Remove Suite(s)     11 
    21 Move on Mobile Home    12 
    22 Move Building OFF Site   98 
    23 Attached Garage/Carport   15 
    24 Excavation      04 
    25 Footing & Foundation   04 
    26 New House      01 
    27 Structural Frame    05 
    28 231        23 

,這裏是表

SELECT DISTINCT JOB_CATEGORY, WORK_TYPE FROM edmonton_upto_10may2016_mostly_text; 

JOB_CATEGORY        WORK_TYPE 
Accessory Building Combination    (01) New 
Other Miscellaneous Building     (03) Interior Alterations 
Commercial Demolition      (99) Demolition 
Other Miscellaneous Building     (03) Exterior Alterations 
Uncovered Deck Combination     (03) Deck Attached 
Rowhousing & Semi-Detachment     (01) Building - New 
Other Miscellaneous Building     (99) Demolition 
Other Miscellaneous Building     (07) Add Suites to Single Dwelling 
House Combination       (01) New 
Commercial Final        (03) Interior Alterations 
Commercial Final        (03) Exterior Alterations 
Commercial Final        (02) Addition 
Commercial Footing/Foundation    (04) Foundation 
Other Miscellaneous Building     (02) Addition 
Commercial Final        (01) New 
Other Miscellaneous Building     (14) Hot Tub 
Commercial Final  
Other Miscellaneous Building     (01) Building - New 
Other Miscellaneous Building     (03) Deck Attached 
Commercial Structural Framing    (05) Structure 
Other Miscellaneous Building     (12) Move Building on to Site 
Commercial Final        (12) Move Building OnSite 
Commercial Final        (08) Add Suites to Multi-Dwelling 
Mobile Home Move On 
Commercial Final        (09) Convert Non-Res to Residential 
Other Miscellaneous Building     (14) Swimming Pool 
Other Miscellaneous Building     (11) Remove Suites 
Commercial Final        (10) Convert Residential to Non-Res 
Other Miscellaneous Building     (04) Foundation 
Commercial Excavation      (04) Foundation 
Commercial Final        (11) Remove Suite(s) 
Uncovered Deck Combination     (03) Exterior Alterations 
Commercial Final        (99) Demolition 
Other Miscellaneous Building     (12) Move on Mobile Home 
Other Miscellaneous Building     (98) Move Building OFF Site 
Other Miscellaneous Building     (15) Attached Garage/Carport 
Commercial Final        (04) Excavation 
Other Miscellaneous Building     (08) Add Suites to Multi-Dwelling 
Commercial Final        (04) Footing & Foundation 
Rowhousing & Semi-Detachment     (01) New House 
Rowhousing & Semi-Detachment     (04) Foundation 
Commercial Final        (05) Structural Frame 
Uncovered Deck Combination     (02) Addition 
Commercial Final        231 
Other Miscellaneous Building     (01) New House 

SO後edmonton_upto_10may2016_mostly_text ...

什麼,沒有填充,當WORK_TYPE爲NULL時。有沒有辦法讓它填充,即使它是空的?

運行查詢後,我得到了:

JOB-CATEGORY      workd_id 
Accessory Building Combination  1 
Commercial Demolition    3 
Commercial Excavation    9 
Commercial Final     1 
Commercial Footing/Foundation 9 
Commercial Structural Framing  12 
House Combination     1 
Other Miscellaneous Building  2 
Rowhousing & Semi-Detachment  6 
Uncovered Deck Combination   4 

和移動主頁繼續前行沒有在Job_Category流行,因爲

ON WORK_TYPE = CONCAT( '(',work_num, ')', '',work_desc)

想出了一個NULL值....但在work_type表空了的ID ...

謝謝!嘗試,因爲我去

+0

爲什麼你用'group by'選擇'distinct distinct'?這個構造幾乎從來沒有道理。 –

回答

0

我認爲你正在尋找一個outer join來了解這一點:

INSERT INTO permits.JOB_CATEGORY(jc_desc, WORK_TYPE_workd_id) 
    SELECT DISTINCT 
     JOB_CATEGORY, workd_id 
    FROM edmonton_upto_10may2016_mostly_text 
    LEFT OUTER JOIN WORK_TYPE 
    ON WORK_TYPE = CONCAT('(',work_num,')',' ',work_desc) 
    GROUP BY JOB_CATEGORY; 

這樣你的主表(左側在這個例子 - edmonton_upto_10may2016_mostly_text)總是返回,無論在右側表WORK_TYPE

+0

太棒了!非常感謝你,我知道這件事很簡單......出於某種原因,我只是想不起來,謝謝你解釋以及我想理解爲什麼編碼更好:) – Erilyn

+0

@Erilyn太棒了!將此標記爲答案如何? ; ^) –

+0

嗨,所以這個查詢工作...但是我現在遇到了另一個問題! – Erilyn

1

匹配的記錄,要回答你的問題的: 可以實現通過使用left outer join代替join由於Android冰火提到你的目標。

將來可能遇到的另一個問題可能是 函數在函數的任何輸入爲NULL時導致NULL。你可以做的是使用concat函數中的ifnull函數。

的主要問題,但是,我看到在數據庫方案。根據您的定義,定義列WORK_TYPE通常不是一個好方法,因爲它不是原子的。所以即使是第一種正常形式也是如此,結果是,你需要解決那些伴隨而來的問題。你應該列WORK_TYPE分成兩列,在表WORK_TYPE。 (命名約定顯然也不是很好)。OR mayby更好的形式給出了將不會有work_numberedmonton_upto_10may2016_mostly_text可言,因爲它似乎並不認爲你需要它複製那裏,你可以隨時加入這個值。

+0

非常感謝你的幫助! – Erilyn

+0

是的,命名約定有點......令人困惑的是,大約有2位DBA正在處理這個問題,所以當你頭腦中有一個想法,並且別人有別的東西,而且我們溝通不好時,它的刺激性會反覆出現。 。非常感謝你的解釋,因爲我試圖理解編碼背後的原因:) – Erilyn