MySql - 加入不给出预期结果

时间:2016-04-15 02:20:29

标签: mysql join

尝试在查询下方运行,希望找到匹配项。由于我的两个子查询是相同的,我希望找到匹配,这不会发生在这里。

select TAB_1.RUL_IDD, TAB_2.RUL_IDD
FROM
(   select aaa.RUL_ID RUL_IDD from TEST_1 aaa 
    inner join 
    (
    select dt,@curRank := @curRank + 1 AS rank
    from (
        select distinct DATE(BTCH_RUN_DTTM) dt
        FROM      TEST_1 
        where       ALRT_FLG_IND = 'Y' 
                    and PASS_IND = 'N' 
                    and SCH_RUN_IND = 'Y'
        order by DATE(BTCH_RUN_DTTM) desc
         ) p,
        (SELECT @curRank := 0) r
    order by dt desc
    ) zz 
    where DATE(aaa.BTCH_RUN_DTTM) = zz.dt
    and zz.rank=1
    and aaa.ALRT_FLG_IND = 'Y' 
    and aaa.PASS_IND = 'N' 
    and aaa.SCH_RUN_IND = 'Y'
) TAB_1
left outer JOIN 
(
    select bbb.RUL_ID as RUL_IDD from TEST_1 bbb 
    inner join 
    (
    select dt,@curRank := @curRank + 1 AS rank
    from (
        select distinct DATE(BTCH_RUN_DTTM) dt
        FROM      TEST_1 
        where       ALRT_FLG_IND = 'Y' 
                    and PASS_IND = 'N' 
                    and SCH_RUN_IND = 'Y'
        order by DATE(BTCH_RUN_DTTM) desc
        ) p,
        (SELECT @curRank := 0) r
    order by dt desc
    ) zzz 
    on DATE(bbb.BTCH_RUN_DTTM) = zzz.dt
    and zzz.rank=1
    and bbb.ALRT_FLG_IND = 'Y' 
    and bbb.PASS_IND = 'N' 
    and bbb.SCH_RUN_IND = 'Y'
)  TAB_2
on TAB_2.RUL_IDD = TAB_1.RUL_IDD;

结果: enter image description here

没有得到它没有给出预期结果的原因。 TAB_2.RUL_IDD也应与TAB_1.RUL_IDD具有相同的值。专家可以帮助我吗?

0 个答案:

没有答案