LEFT JOIN 4表但显示多个值

时间:2018-04-02 02:12:04

标签: mysql left-join

我有4个表,其中一些表有超过1个值。我需要的是总和但当前查询显示重复

表1 tb_satker

kode_satker     nama_satker
412772  KANTOR PUSAT DITJEN HUBLA
412781  OP UTAMA TANJUNG PRIOK
412797  KSOP SUNDA KELAPA
413422  KESYAHBANDARAN UTAMA TANJUNG PRIOK
606301  BALAI KESEHATAN KERJA PELAYARAN
652474  KSOP KEPULAUAN SERIBU
652481  KSOP MUARA BARU
652495  KSOP MUARA KARANG
652500  KSOP KALIBARU
652517  KSOP MARUNDA

表2 tb_target

id  kode_satker     jumlah
1   412772  500

表3 tb_simponi

id  kode_satker     jumlah
1   412772  100
2   412772  200

表4 tb_pagu

id  kode_satker     sumber  jumlah
1   412772  D   1
2   412772  D   2

我所做的是http://sqlfiddle.com/#!9/93c7ac/1

SELECT
    tb_satker.nama_satker,
    tb_target.jumlah as target,
    sum(tb_simponi.jumlah) as realisasi,
    sum(tb_pagu.jumlah) as pagu
FROM tb_satker
LEFT JOIN tb_target
    ON tb_satker.kode_satker = tb_target.kode_satker
LEFT JOIN tb_simponi
    ON tb_satker.kode_satker = tb_simponi.kode_satker
LEFT JOIN tb_pagu
    ON tb_satker.kode_satker = tb_pagu.kode_satker
GROUP BY
    tb_satker.kode_satker

但它显示

nama_satker     target  realisasi   pagu
KANTOR PUSAT DITJEN HUBLA   500     600     6
OP UTAMA TANJUNG PRIOK  (null)  (null)  (null)
KSOP SUNDA KELAPA   (null)  (null)  (null)
KESYAHBANDARAN UTAMA TANJUNG PRIOK  (null)  (null)  (null)
BALAI KESEHATAN KERJA PELAYARAN     (null)  (null)  (null)
KSOP KEPULAUAN SERIBU   (null)  (null)  (null)
KSOP MUARA BARU     (null)  (null)  (null)
KSOP MUARA KARANG   (null)  (null)  (null)
KSOP KALIBARU   (null)  (null)  (null)
KSOP MARUNDA    (null)  (null)  (null)

我期待结果显示

nama_satker     target  realisasi   pagu
KANTOR PUSAT DITJEN HUBLA   500     300     3
OP UTAMA TANJUNG PRIOK  (null)  (null)  (null)
KSOP SUNDA KELAPA   (null)  (null)  (null)
KESYAHBANDARAN UTAMA TANJUNG PRIOK  (null)  (null)  (null)
BALAI KESEHATAN KERJA PELAYARAN     (null)  (null)  (null)
KSOP KEPULAUAN SERIBU   (null)  (null)  (null)
KSOP MUARA BARU     (null)  (null)  (null)
KSOP MUARA KARANG   (null)  (null)  (null)
KSOP KALIBARU   (null)  (null)  (null)
KSOP MARUNDA    (null)  (null)  (null)

1 个答案:

答案 0 :(得分:0)

在学习联接时,似乎人们常常会犯这样的错误,即它可能会导致聚合不能产生预期的结果。在您的情况下,看起来有些行被覆盖,可能是由于连接。一种解决方法是在单独的子查询中执行聚合。

// check if a form was submitted
if( !empty( $_POST ) ){
    // convert form data to json format
    $postArray = array(
      "driver" => $_POST['driver'],
      "animation" => array(
        "typename" => $_POST["typename"],
        "tail" => $_POST["tail"],
        "growthRate" => $_POST["growthRate"]
      ),
      "run" => array(
        "fps" => $_POST["fps"]
      ),
      "layout" => $_POST['layout']  
    );
    //you might need to process any other post fields you have..

    $json = json_encode( $postArray );
    // make sure there were no problems
    //if( json_last_error() != JSON_ERROR_NONE ){
        //exit;  // do your error handling here instead of exiting
    // }
    $file = '/home/pi/Documents/webtest1.json';
    // write to file
    //   note: _server_ path, NOT "web address (url)"!
    file_put_contents( $file, $json);
}   
shell_exec("sudo killall bp");
shell_exec("sudo bp /home/pi/Documents/webtest1.json > /dev/null 2>/dev/null &");
header("Location: http://www.bbc.co.uk");
die();

请注意,您的SELECT t1.nama_satker, t2.jumlah AS target, COALESCE(t3.realisasi, 0) AS realisasi, COALESCE(t4.pagu, 0) AS pagu FROM tb_satker t1 LEFT JOIN tb_target t2 ON t1.kode_satker = t2.kode_satker LEFT JOIN ( SELECT kode_satker, SUM(jumlah) AS realisasi FROM tb_simponi GROUP BY kode_satker ) t3 ON t1.kode_satker = t3.kode_satker LEFT JOIN ( SELECT kode_satker, SUM(jumlah) AS pagu FROM tb_pagu GROUP BY kode_satker ) t4 ON t1.kode_satker = t4.kode_satker; 语法也存在问题;您是按一列聚合但是选择其他非聚合列,这没有逻辑意义。在上面的重构版本中,这不是问题,因为我们不再需要在外部查询中使用GROUP BY