当连接表缺少记录时,如何返回表中的所有记录

时间:2016-05-17 01:36:28

标签: mysql

所以我认为我再次过度思考,似乎无法找到解决方案。另外,我认为我的自动增量在数据库中无法完全发挥作用。 Anywho,我正在尝试加入2个表,并使用" trimmed"列匹配另一列并检索2列...对不起,如果这是非常混乱。这是我的尝试:

PS ... IT工作或多或少,但是当条件为空时,它不会返回任何内容....我如何才能使它仍然返回?在另一个表中仍然满足条件,即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

这里是表edmonton_upto_10may2016_mostly_text 之后

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

没有填充的是,当WORK_TYPE为NULL时。有没有办法让它填充,即使它是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

和Mobile Home Move On没有弹出Job_Category,因为

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

显示为NULL值....但是work_type表中的null有一个ID ...

谢谢!试着理解这一点以及我去

2 个答案:

答案 0 :(得分:1)

回答你的问题: 您可以使用left outer join而不是join作为Android Addict提及来实现目标。

您将来可能遇到的另一个问题可能是 如果函数的任何输入为NULL,则concat函数将返回NULL。你可以做的是使用ifnull函数中的concat函数。

然而,我在数据库方案中看到了主要问题。由于它不是原子的,所以定义列 WORK_TYPE 通常不是一个好的方法。因此,即使第一个正常形式被打破,结果是,您需要解决随之而来的问题。您应该将列 WORK_TYPE 拆分为两列,如表 WORK_TYPE 中所示。 (命名惯例显然也不是很好)。 OR 可能更好的方法是不要在表 edmonton_upto_10may2016_mostly_text 中使用 work_number ,因为它似乎并不需要在那里复制它你总能加入这个价值。

答案 1 :(得分: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