1

我有 3 个关系表

1. 表权限(从用户名获取用户权限)

select branch_code,username from privilege where username='joko';

结果:

| branch_code  | username |
|--------------|----------|
| 'IDJK003MPV' | 'joko'   |
| 'IDJK001MAG' | 'joko'   |
| 'IDJK002MCL' | 'joko'   |
| 'IDBTNCTR'   | 'joko'   |

2. 表 biodata_karyawan(这个表关于员工的 branch_code)

select branch_code,status_karyawan from biodata_karyawan;

结果:

| branch_code  | status_karyawan |
|--------------|-----------------|
| 'IDJB001BEC' | 'Aktif'         |
| 'IDJB001BEC' | 'Aktif'         |
| 'IDPA001PPA' | 'Aktif'         |
| 'IDJK001MAG' | 'Aktif'         |
| 'IDJB001BEC' | 'Aktif'         |
| 'IDJB001BEC' | 'Tidak Aktif'   |
| 'IDBTNCTR'   | 'Tidak Aktif'   |

3.表分支

select branch_code,branch_name from branch;

结果 :

| branch_code  | branch_name              |
|--------------|--------------------------|
| 'IDJB001BEC' | 'BEC BANDUNG'            |
| 'IDJK001MAG' | 'MALL ARTHA GADING'      |
| 'IDJK001HO'  | 'HEAD OFFICE'            |
| 'IDPA001PPA' | 'MALL MATAHARI JAYAPURA' |
| 'IDBE001BGK' | 'BENCOOLEN INDAH MALL'   |
| 'IDJK002MPB' | 'METRO PASAR BARU'       |

我希望结果会是这样的家伙:(记录只是样本)

| branch_code  | branch_name    | count |
|--------------|----------------|-------|
| 'IDJB001BEC' | 'MALL BANDUNG' | '5'   |
| 'IDJK001MLP' | 'MALL LIPPO'   | '2'   |
| 'IDJK002MPI' | 'MALL PURI'    | '0'   |
| 'IDJB002ZZZ' | 'MALL POSO'    | '0'   |

谁能帮助我?
谢谢你的时间。

4

1 回答 1

2

在 status_karyawan 上使用 count 然后分组

select bk.branch_code,b.branch_name,count(bk.status_karyawan) from
  biodata_karyawan bk join
  branch b on bk.branch_code = b.branch_code
  group by bk.branch_code,b.branch_name

这是sql小提琴

根据您的评论更新查询

select p.branch_code,b.branch_name,count(bk.status_karyawan) from
 biodata_karyawan bk join
 branch b on bk.branch_code = b.branch_code
 join privilege p on b.branch_code = p.branch_code
 Where p.username = 'joko'
 group by p.branch_code,b.branch_name

更新小提琴

于 2013-11-14T09:06:28.157 回答