The result I imagine to have are the COMMON records of both sources and the records of a
who are not in b
but I am only shown the records in common.
In the case of LEFT JOIN joins care should be taken when using "right table" columns in the WHERE clause. Otherwise, LEFT JOIN is transformed internally into INNER JOIN.
The following excerpt in your code:
and b.mnp_id = 9
made LEFT JOIN become INNER JOIN, then disappeared with the 266 and others...
Try:
-- código #1
SELECT A.colunas, B.colunas
from banco1.sistema.tb_menus_perfil_params as A
left join banco2.sistema.tb_menus_perfil_params as B
on A.pm_id = B.pm_id
where A.mnp_id = 10
and (B.mnp_id = 9 or B.pm_id is null);
Other option:
-- código #2
SELECT A.colunas, B.colunas
from banco1.sistema.tb_menus_perfil_params as A
left join banco2.sistema.tb_menus_perfil_params as B
on A.pm_id = B.pm_id
and B.mnp_id = 9
where A.mnp_id = 10;
And one more option:
-- código #3
with
B1_tb_menus_perfil_params as (
SELECT *
from banco1.sistema.tb_menus_perfil_params
where mnp_id = 10
),
B2_tb_menus_perfil_params as (
SELECT *
from banco2.sistema.tb_menus_perfil_params
where mnp_id = 9
)
SELECT A.colunas, B.colunas
from B1_tb_menus_perfil_params as A
left join B2_tb_menus_perfil_params as B
on A.pm_id = B.pm_id;
In the 3 codes above you must replace colunas
by the name of the columns you need.
Related: https://answall.com/questions/348273/com-left-join-listar-os-not-in/348286#348286
– novic