如何从多个连接表MySQL中选择不同的最大值

时间:2016-05-24 08:28:46

标签: mysql phpmyadmin

这是我当前选择数据的查询:

select a.No_Registrasi, a.Nama_CTKI, b.Nama_Negara, c.ID_Rad, d.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

结果如下:
query result

我如何区分那些ID_Rad和ID_Lab,我需要每个ID_Rad和ID_Lab的最大值,所以它将是这样的:
result

1 个答案:

答案 0 :(得分:1)

使用GROUP BYMAX,试试这个;)

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