SQL : Left Join multiple time the same table with different types , Duplicates lines adding the value in it
SQL : Left Join multiple time the same table with different types , Duplicates lines adding the value in it
我的问题是这个查询:
select
distinct
o1.id_personne,
o1.id_table,
tel.li_num_tel as Telephone,
mail.li_url as EMail,
fax.li_num_tel as Fax
from table o1
Inner join personne p1 on
o1.id_personne = p1.id_personne
left join table_adresse a1 on
o1.id_table = a1.id_table and
a1.co_type_adresse like 'TEL'
left join table_adresse a2 on
o1.id_table = a2.id_table and
a2.co_type_adresse like 'NUM'
left join tabletel tel on
a1.id_table_tel = tel.id_table_tel and
tel.id_nature_adrtel = 1502
left join tabletel fax on
a1.id_table_tel = fax.id_table_tel and
fax.id_nature_adrtel = 1500
left join tablenum mail on
a2.id_table_num = mail.id_table_num and
mail.id_nature_adrnum = 1400
where
p1.id_personne=1
这个结果:
|id_personne | id_table | telephone | email | Fax |
|1 | 5 | null |test@te|null |
|1 | 9 | null |test@te|555 89|
|1 | 9 | 123 123 5 |test@te| null |
id 5 的值是正确的,但 id 9 与不同的值重复,它应该return同一行的 3 个(电子邮件 + 电话 + 传真)字段
看起来您在 table table_adresse
中有两行 id_table = 9 AND co_type_adresse like 'TEL'
,具有不同的 li_num_tel
值。每个将与 table join personne
的同一行组合以产生结果行。因为结果行不同,所以都通过 DISTINCT
选择。
以下可能有效。函数 MAX
应忽略您的空值,因此在将 table_adresse 加入电话和传真表时合并重复的行
SELECT
distinct
o1.id_personne,
o1.id_table,
mail.li_url as EMail,
X.li_num_tel as Fax
X.li_num_tel as Telephone,
from
table o1
Inner join personne p1 on
o1.id_personne = p1.id_personne
left join table_adresse a2 on
o1.id_table = a2.id_table and
a2.co_type_adresse like 'NUM'
left join tablenum mail on
a2.id_table_num = mail.id_table_num and
mail.id_nature_adrnum = 1400
LEFT JOIN
(
SELECT
a1.id_table,
MAX(tel.li_num_tel) as Telephone,
MAX(fax.li_num_tel) as Fax
FROM table_adresse a1
LEFT JOIN tabletel tel on
a1.id_table_tel = tel.id_table_tel and
tel.id_nature_adrtel = 1502
LEFT JOIN tabletel fax on
a1.id_table_tel = fax.id_table_tel and
fax.id_nature_adrtel = 1500
WHERE co_type_adresse like 'TEL'
GROUP BY a1.id_table
) X
ON
o1.id_table = X.id_table
WHERE
p1.id_personne=1
...或者在您的原始查询中,您可以试试这个:
select
o1.id_personne,
o1.id_table,
MAX(tel.li_num_tel) as Telephone,
MAX(mail.li_url) as EMail,
MAX(fax.li_num_tel) as Fax
from table o1
Inner join personne p1 on
o1.id_personne = p1.id_personne
left join table_adresse a1 on
o1.id_table = a1.id_table and
a1.co_type_adresse like 'TEL'
left join table_adresse a2 on
o1.id_table = a2.id_table and
a2.co_type_adresse like 'NUM'
left join tabletel tel on
a1.id_table_tel = tel.id_table_tel and
tel.id_nature_adrtel = 1502
left join tabletel fax on
a1.id_table_tel = fax.id_table_tel and
fax.id_nature_adrtel = 1500
left join tablenum mail on
a2.id_table_num = mail.id_table_num and
mail.id_nature_adrnum = 1400
where
p1.id_personne=1
GROUP BY
o1.id_personne,
o1.id_table
我的问题是这个查询:
select
distinct
o1.id_personne,
o1.id_table,
tel.li_num_tel as Telephone,
mail.li_url as EMail,
fax.li_num_tel as Fax
from table o1
Inner join personne p1 on
o1.id_personne = p1.id_personne
left join table_adresse a1 on
o1.id_table = a1.id_table and
a1.co_type_adresse like 'TEL'
left join table_adresse a2 on
o1.id_table = a2.id_table and
a2.co_type_adresse like 'NUM'
left join tabletel tel on
a1.id_table_tel = tel.id_table_tel and
tel.id_nature_adrtel = 1502
left join tabletel fax on
a1.id_table_tel = fax.id_table_tel and
fax.id_nature_adrtel = 1500
left join tablenum mail on
a2.id_table_num = mail.id_table_num and
mail.id_nature_adrnum = 1400
where
p1.id_personne=1
这个结果:
|id_personne | id_table | telephone | email | Fax |
|1 | 5 | null |test@te|null |
|1 | 9 | null |test@te|555 89|
|1 | 9 | 123 123 5 |test@te| null |
id 5 的值是正确的,但 id 9 与不同的值重复,它应该return同一行的 3 个(电子邮件 + 电话 + 传真)字段
看起来您在 table table_adresse
中有两行 id_table = 9 AND co_type_adresse like 'TEL'
,具有不同的 li_num_tel
值。每个将与 table join personne
的同一行组合以产生结果行。因为结果行不同,所以都通过 DISTINCT
选择。
以下可能有效。函数 MAX
应忽略您的空值,因此在将 table_adresse 加入电话和传真表时合并重复的行
SELECT
distinct
o1.id_personne,
o1.id_table,
mail.li_url as EMail,
X.li_num_tel as Fax
X.li_num_tel as Telephone,
from
table o1
Inner join personne p1 on
o1.id_personne = p1.id_personne
left join table_adresse a2 on
o1.id_table = a2.id_table and
a2.co_type_adresse like 'NUM'
left join tablenum mail on
a2.id_table_num = mail.id_table_num and
mail.id_nature_adrnum = 1400
LEFT JOIN
(
SELECT
a1.id_table,
MAX(tel.li_num_tel) as Telephone,
MAX(fax.li_num_tel) as Fax
FROM table_adresse a1
LEFT JOIN tabletel tel on
a1.id_table_tel = tel.id_table_tel and
tel.id_nature_adrtel = 1502
LEFT JOIN tabletel fax on
a1.id_table_tel = fax.id_table_tel and
fax.id_nature_adrtel = 1500
WHERE co_type_adresse like 'TEL'
GROUP BY a1.id_table
) X
ON
o1.id_table = X.id_table
WHERE
p1.id_personne=1
...或者在您的原始查询中,您可以试试这个:
select
o1.id_personne,
o1.id_table,
MAX(tel.li_num_tel) as Telephone,
MAX(mail.li_url) as EMail,
MAX(fax.li_num_tel) as Fax
from table o1
Inner join personne p1 on
o1.id_personne = p1.id_personne
left join table_adresse a1 on
o1.id_table = a1.id_table and
a1.co_type_adresse like 'TEL'
left join table_adresse a2 on
o1.id_table = a2.id_table and
a2.co_type_adresse like 'NUM'
left join tabletel tel on
a1.id_table_tel = tel.id_table_tel and
tel.id_nature_adrtel = 1502
left join tabletel fax on
a1.id_table_tel = fax.id_table_tel and
fax.id_nature_adrtel = 1500
left join tablenum mail on
a2.id_table_num = mail.id_table_num and
mail.id_nature_adrnum = 1400
where
p1.id_personne=1
GROUP BY
o1.id_personne,
o1.id_table