MySql:左连接2个表

时间:2013-11-19 02:42:42

标签: mysql sql left-join

对不起...... 我有问题。 这是我的表:

1。 Biodata_karyawan

`nik` varchar(10) NOT NULL,
    `status_karyawan` varchar(11) NOT NULL,
    `site_code` varchar(20) NOT NULL,
    `jabatan` varchar(20) NOT NULL

2。站点

`site_name` varchar(40) NOT NULL,
    `site_code` varchar(10) NOT NULL,
 `status` enum('a','i') NOT NULL,
 `anggota` int(2) NOT NULL,
 `danru` int(2) NOT NULL,
 `wadanru` int(2) NOT NULL,
 `secwan` int(2) NOT NULL,
 `pkd` int(2) NOT NULL,
 `danki` int(2) NOT NULL,
 `admin` int(2) NOT NULL,
 `resepsionis` int(2) NOT NULL,
 `lidik` int(2) NOT NULL,
 `ass_chief` int(2) NOT NULL,
 `chief` int(2) NOT NULL,
 `kordinator` int(2) NOT NULL,

这是我的疑问:

select 
    s.site_code,
    sum(S.ANGGOTA) AS komposisi_ANGGOTA,sum(S.DANRU) AS komposisi_DANRU,sum(S.WADANRU) AS komposisi_WADANRU,sum(S.SECWAN) AS komposisi_SECWAN,
    sum(S.PKD) AS komposisi_PKD,sum(S.DANKI) AS komposisi_DANKI,sum(S.ADMIN) AS komposisi_ADMIN,
    sum(S.RESEPSIONIS) AS komposisi_RESEPSIONIS,sum(S.LIDIK) AS komposisi_LIDIK,sum(S.ASS_CHIEF) AS komposisi_ASS_CHIEF,
    sum(S.CHIEF) AS CHIEF,sum(S.KORDINATOR) AS KORDINATOR,
    SUM(CASE BK.jabatan WHEN 'anggota' THEN 1 ELSE 0 END) AS personil_anggota,
    SUM(CASE BK.jabatan WHEN 'danru' THEN 1 ELSE 0 END) AS personil_danru,
    SUM(CASE BK.jabatan WHEN 'wadanru' THEN 1 ELSE 0 END) AS personil_wadanru,
    SUM(CASE BK.jabatan WHEN 'secwan' THEN 1 ELSE 0 END) AS personil_secwan,
    SUM(CASE BK.jabatan WHEN 'pkd' THEN 1 ELSE 0 END) AS personil_pkd,
    SUM(CASE BK.jabatan WHEN 'danki' THEN 1 ELSE 0 END) AS personil_danki,
    SUM(CASE BK.jabatan WHEN 'admin' THEN 1 ELSE 0 END) AS personil_admin,
    SUM(CASE BK.jabatan WHEN 'resepsionis' THEN 1 ELSE 0 END) AS personil_resepsionis,
    SUM(CASE BK.jabatan WHEN 'lidik' THEN 1 ELSE 0 END) AS personil_lidik,
    SUM(CASE BK.jabatan WHEN 'ass_chief' THEN 1 ELSE 0 END) AS personil_ass_chief,
    SUM(CASE BK.jabatan WHEN 'chief' THEN 1 ELSE 0 END) AS personil_chief
    FROM BIODATA_KARYAWAN BK,site s
    WHERE bk.status_karyawan='Aktif'
    AND s.status='a'
    GROUP BY s.site_code
    ORDER BY s.site_code asc;

我的期望可以这样显示(数据只是样本) - > 更新

|site_code    |komposisi_anggota|komposisi_danru|komposisi_wadanru|komposisi_secwan|komposisi_pkd|komposisi_danki|komposisi_admin|komposisi_resepsionis|komposisi_lidik|komposisi_ass_chief|komposisi_chief|komposisi_kordinator|personil_anggota|personil_danru|personil_wadanru|personil_secwan|personil_pkd|personil_danki|personil_admin|personil_resepsionis|kpersonil_lidik|personil_ass_chief|personil_chief|personil_kordinator|   
|-------------|-----------------|---------------|-----------------|----------------|-------------|---------------|---------------|---------------------|---------------|-------------------|---------------|--------------------|-----------------|-------------|----------------|---------------|-------------|---------------|---------------|---------------------|---------------|-------------------|---------------|-------------|   
| IDJK001BEC  | '50'              | '3'                     | '0'                           |'0'                         |'1'                    |'1'            |'5'            |'1'                  |'11'           |'0'                |'0'            |'0'                 | '50'            | '3'                 | '0'                    |'0'                        |'1'                    |'1'            |'5'            |'1'                  |'11'           |'0'                |'0'            | '1'         |    
| IDJK001AIG  | '50'              | '3'                     | '0'                           |'0'                         |'1'                    |'1'            |'5'            |'1'                  |'11'           |'0'                |'0'            |'0'                 | '50'            | '3'                 | '0'                    |'0'                        |'1'                    |'1'            |'5'            |'1'                  |'11'           |'0'                |'0'            | '1'         |    

问题 结果不是有效数据。

SQL FIDDLE:http://sqlfiddle.com/#!2/b11aae

感谢你的时间大师。

2 个答案:

答案 0 :(得分:0)

BK和s之间没有JOIN条件。

你能试试吗?

FROM BIODATA_KARYAWAN BK LEFT JOIN site s USING(site_code)
WHERE bk.status_karyawan='Aktif'
    AND s.status='a'

答案 1 :(得分:0)

Horee ......我试过:

这是答案:

select
    `s`.`site_code` AS `site_code`,
    `s`.`site_name` AS `site_name`,
    `s`.`anggota` AS `komposisi_anggota`,
    `s`.`danru` AS `komposisi_danru`,
    `s`.`wadanru` AS `komposisi_wadanru`,
    `s`.`secwan` AS `komposisi_secwan`,
    `s`.`pkd` AS `komposisi_pkd`,
    `s`.`danki` AS `komposisi_danki`,
    `s`.`admin` AS `komposisi_admin`,
    `s`.`resepsionis` AS `komposisi_resepsionis`,
    `s`.`lidik` AS `komposisi_lidik`,
    `s`.`ass_chief` AS `komposisi_assistant_chief`,
    `s`.`chief` AS `komposisi_chief`,
    `s`.`kordinator` AS `komposisi_kordinator`,
    sum((case `bk`.`jabatan` when 'anggota' then 1 else 0 end)) AS `personil_anggota`,
    sum((case `bk`.`jabatan` when 'danru' then 1 else 0 end)) AS `personil_danru`,
    sum((case `bk`.`jabatan` when 'wadanru' then 1 else 0 end)) AS `personil_wadanru`,
    sum((case `bk`.`jabatan` when 'secwan' then 1 else 0 end)) AS `personil_secwan`,
    sum((case `bk`.`jabatan` when 'pkd' then 1 else 0 end)) AS `personil_pkd`,
    sum((case `bk`.`jabatan` when 'danki' then 1 else 0 end)) AS `personil_danki`,
    sum((case `bk`.`jabatan` when 'admin' then 1 else 0 end)) AS `personil_admin`,
    sum((case `bk`.`jabatan` when 'resepsionis' then 1 else 0 end)) AS `personil_resepsionis`,
    sum((case `bk`.`jabatan` when 'lidik' then 1 else 0 end)) AS `personil_lidik`,
    sum((case `bk`.`jabatan` when 'ass_chief' then 1 else 0 end)) AS `personil_assistant_chief`,
    sum((case `bk`.`jabatan` when 'chief' then 1 else 0 end)) AS `personil_chief`,
    sum((case `bk`.`jabatan` when 'kordinator' then 1 else 0 end)) AS `personil_kordinator` 
    from (`site` `s` left join `biodata_karyawan` `bk` 
    on(((`s`.`site_code` = `bk`.`site_code`) and (`bk`.`status_karyawan` = 'Aktif') and (`s`.`status` = 'a'))))
    group by `s`.`site_code` order by `s`.`site_code`;