MySQL没有返回null列的连接值

时间:2016-05-25 06:46:50

标签: mysql

我有这4张桌子:
tables

我的预期结果:
expected result

这是我当前的查询(来自here

select a.No_Registrasi, a.Nama_CTKI, b.Nama_Negara, CONCAT('RA-', MAX(substring(c.ID_Rad, 4) + 0)) AS ID_Rad, CONCAT('Lab-',MAX(substring(d.ID_Lab, 5) + 0)) AS Id_Lab
FROM tb_registrasi a 
JOIN tb_negara_tujuan b
    ON a.ID_Negara = b.ID_Negara
JOIN tb_radiologi c
   ON a.No_Registrasi = c.No_Registrasi
JOIN tb_laboratorium d
   ON a.No_Registrasi = d.No_Registrasi
GROUP BY a.No_Registrasi, a.Nama_CTKI, b.Nama_Negara

这是查询的结果:
enter image description here

我如何得到我期待的结果?我在这样的高级查询中真的没有经验,任何帮助都是非常苛刻的。谢谢。

1 个答案:

答案 0 :(得分:1)

希望这有帮助。

select a.No_Registrasi, a.Nama_CTKI, b.Nama_Negara, CONCAT('RA-', MAX(substring(c.ID_Rad, 4) + 0)) AS ID_Rad, CONCAT('Lab-',MAX(substring(d.ID_Lab, 5) + 0)) AS Id_Lab
FROM tb_registrasi a 
LEFT JOIN tb_negara_tujuan b
    ON a.ID_Negara = b.ID_Negara
LEFT JOIN tb_radiologi c
   ON a.No_Registrasi = c.No_Registrasi
LEFT JOIN tb_laboratorium d
   ON a.No_Registrasi = d.No_Registrasi
GROUP BY a.No_Registrasi, a.Nama_CTKI, b.Nama_Negara