【问题标题】:Informix multiple joinsInformix 多连接
【发布时间】:2012-06-27 16:38:50
【问题描述】:

我需要修改我的查询以添加其他联接。但是新的联接可能需要是左联接。我不确定该怎么做。我正在使用 Informix:

     set    query   "SELECT DISTINCT x.xfertype,x.app_type,x.service_type,x.lang,x.area_type,x.area_value,x.module,x.field1,x.field2,x.disabled,a.frames,a.allocs,a.term_id,t.term,c.center_id,c.center_name,a.message_id,x.field3,x.apn_type,x.global, a.icm, s.group_name "
    append  query   " FROM test_xfertypes AS x, test_allocation AS a, test_terms AS t, test_callcenter AS c"
    append  query   " AND a.xfertype = x.xfertype "
    append  query   " AND a.term_id = t.term_id "
    append  query   " AND t.center_id = c.center_id ";

test_xfertypes AS x 包含area_value (int)

我想左加入上面的表和另一个新表test_routing_groups AS s

我想离开加入,让它返回s.group_name WHERE x.area_value IN (s.area_id);如果 group_name 存在,则返回 group_name,否则返回 null。

【问题讨论】:

    标签: sql join left-join informix


    【解决方案1】:

    您需要为此使用标准连接语法。您的查询应如下所示:

    SELECT DISTINCT x.xfertype, x.app_type, x.service_type, x.lang,x.area_type,
           x.area_value, x.module, x.field1, x.field2, x.disabled,
           a.frames, a.allocs, a.term_id,
           t.term,c.center_id, c.center_name,a.message_id, x.field3, x.apn_type,x.global,
           a.icm, s.group_name
    FROM test_xfertypes x join
         test_allocation a
         on a.xfertype = x.xfertype join
         test_terms t
         on a.term_id = t.term_id join
         test_callcenter c
         on t.center_id = c.center_id
    

    您可以通过添加以下内容离开加入另一个表:

    left outer join test_routing_groups s
    on x.area_value IN (s.area_id)
    

    您也可以使用“x.area_value = s.area_id”而不是“in”子句。

    【讨论】:

    • +1 因为这个想法是正确的,但我不喜欢隐藏关键关键字(如 JOIN 和 ON)的布局。我会将这些大写并在 FROM 下对齐关键字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多