使用LEFT OUTER JOIN组合2个SELECT查询

时间:2014-04-29 09:26:32

标签: mysql sql entity-framework join left-join

如何使用左连接语法将其与两个选择查询相结合。 (我的查询有错误,我找不到解决方案)

select
    *
from
    (select
        mi.parent_entity_id entity,
        tctp.institution_rec_id institutionRecId,
        institution_code storecode,
        institution_name storename,
        case when sum(unsettled_points) is null
            then coalesce (sum(point_value),0)
            else coalesce
        (sum(unsettled_points),0) end sumpoints
     from
        t_card_transaction_point tctp
     inner join
        m_institution mi on tctp.institution_rec_id=mi.institution_rec_id
     where
        mi.parent_entity_id = 70125 and
        tctp.point_status = 'xy4604'
     group by
        entity,
        institutionRecId,
        storecode,
        storename
    ) storeExpired

left join
    entityExpired on storeExpired.entity=entityExpired.entity

(select
    mpb.institution_rec_id entity,
    tctd.institution_rec_id institutionRecId,
    tctd.card_no cardnumber,
    total_amount_primary totalpoints,
    case when total_unsettled_points is null
        then point_value
        else tctd.total_unsettled_points end
    points
 from
    t_card_transaction_detail tctd inner
 join
    m_point_bucket mpb on mpb.card_no=tctd.card_no
 inner join
    m_institution mi on mi.institution_rec_id=tctd.institution_rec_id
 where
    mpb.total_amount_primary > 1000 and
    tctd.adjustment_date is null
 group by
    entity,
    institutionRecId,
    cardnumber,
    totalpoints,
    points
) entityExpired

1 个答案:

答案 0 :(得分:1)

首先: 我们非常感谢代码的正确缩进/衬里以便于阅读:)

第二: “我的查询有错误”并没有特别说明。

Anywho,回答你的问题:

SQL具有

的操作顺序
  1. 其中
  2. 分组依据
  3. 具有
  4. 选择
  5. 按订单排序
  6. 这意味着在执行select时会创建别名。由于“分组依据”在此之前执行,因此别名不存在 - 这可能是您得到的错误。

    另外,我不确定MySQL是否允许连接别名,这在查询中进一步定义(虽然我可能是错的),所以我会将查询本身移动到连接括号中,并使用在“开启” - 后来的声明。

    示例查询:(未经测试,因为我没有表格)

    select 
    * 
    from (
       select 
         mi.parent_entity_id      as entity
       , tctp.institution_rec_id  as institutionRecId
       , institution_code         as storecode
       , institution_name         as storename
       , case when sum(unsettled_points) is null 
          then coalesce (sum(point_value),0) 
          else coalesce (sum(unsettled_points),0) 
       end                        as sumpoints 
       from t_card_transaction_point tctp 
       inner join m_institution      mi on tctp.institution_rec_id = mi.institution_rec_id 
       where 1=1
       and mi.parent_entity_id = 70125 
       and tctp.point_status = 'xy4604' 
       group by 
         mi.parent_entity_id
       , tctp.institution_rec_id
       , institution_code
       , institution_name
    ) storeExpired
    left join (
       select 
         mpb.institution_rec_id   as entity
       , tctd.institution_rec_id  as institutionRecId
       , tctd.card_no             as cardnumber
       , total_amount_primary     as totalpoints
       , case when total_unsettled_points is null 
          then point_value 
          else tctd.total_unsettled_points 
       end                        as points 
       from t_card_transaction_detail tctd 
       inner join m_point_bucket      mpb  on mpb.card_no=tctd.card_no 
       inner join m_institution       mi   on mi.institution_rec_id=tctd.institution_rec_id 
       where 1=1
       and mpb.total_amount_primary > 1000 
       and tctd.adjustment_date is null 
       group by 
         mpb.institution_rec_id
       , tctd.institution_rec_id
       , tctd.card_no
       , total_amount_primary
       , case when total_unsettled_points is null 
          then point_value 
          else tctd.total_unsettled_points 
       end
    ) entityExpired on storeExpired.entity=entityExpired.entity
    

    修改 我只是谷歌了,你实际上可以在你的群组中使用别名的MySQL语句(在MSSQL中不允许,也不是ANSI标准)。

    但是,在看到您对错误的评论后,可能是因为您在创建子查询之前加入别名entityExpired。我猜测移动子查询,就像我在示例中所做的那样,应该可以工作。