来自另一个内部联接的内部联接

时间:2020-04-09 15:17:33

标签: sql oracle

我有一个包含3个表的数据库:

  • PM_FLX_ENTE
  • PM_ST
  • PM_ANO_SP

我想获取PM_ANO_L.L_ANO的某些值的PM_ANO_SP.L_ANO_SP,其中包含带有'/'的代码号。

SELECT COUNT(RF_INTRN),TRIM(pm_st.c_st),TRIM(pm_ano_l.L_ANO)
    FROM PM_FLX_ENTE
    INNER JOIN PM_ST ON PM_FLX_ENTE.C_ST = PM_ST.C_ST
    INNER JOIN PM_ANO_L ON PM_FLX_ENTE.C_ANO = pm_ano_l.c_ano
    WHERE pm_flx_ente.C_ANO <> '0000'
        AND pm_ano_l.c_lang = 'FR'
    group by TRIM(pm_st.c_st), TRIM(pm_ano_l.L_ANO)
ORDER BY COUNT(RF_INTRN) DESC

sql query result and pm_ano_sp table 你能帮我吗

1 个答案:

答案 0 :(得分:0)

您是否看到某种错误?

请注意,当您执行多个内部联接时,必须满足先前的联接条件才能使其他联接正常工作。

IE

INNER JOIN PM_ST ON PM_FLX_ENTE.C_ST = PM_ST.C_ST

为了使以下内容有效,必须评估为真

INNER JOIN PM_ANO_L ON PM_FLX_ENTE.C_ANO = pm_ano_l.c_ano

要解决此问题,您可能需要将INNER JOIN更改为LEFT JOINs

SELECT TRIM(pm_st.c_st),TRIM(pm_ano_l.L_ANO),COUNT(RF_INTRN)
        FROM PM_FLX_ENTE
        LEFT JOIN PM_ST ON PM_FLX_ENTE.C_ST = PM_ST.C_ST
        LEFT JOIN PM_ANO_L ON PM_FLX_ENTE.C_ANO = pm_ano_l.c_ano
        LEFT JOIN PM_ANO_SP ON PM_ANO_L.L_ANO = pm_ano_sp.l_ano_sp
            WHERE pm_flx_ente.C_ANO <> '0000'
            AND C_LANG = 'FR'
            and pm_ano_l.L_ANO like '%/%'
        GROUP BY pm_st.c_st,pm_ano_l.L_ANO