SQL Count使用WHERE子句连接表

时间:2014-03-12 11:16:59

标签: mysql sql count

我需要一些关于计算数据的帮助。

到目前为止,这是我的查询:

 SELECT COUNT(*) FROM (SELECT COUNT(biodata.Nomor_Induk) as jumData
 FROM biodata 
 INNER JOIN statuspegawai ON biodata.Nomor_Induk = statuspegawai.Nomor_Induk 
 WHERE statuspegawai.bagian='IT' 
 GROUP BY biodata.Nomor_Induk)

我有两张桌子:biodatastatuspegawai

两个表都有Nomor_Induk列。

2 个答案:

答案 0 :(得分:1)

您的查询需要子查询表别名:

select count(*)
FROM (SELECT COUNT(biodata.Nomor_Induk) as jumData
      from biodata INNER JOIN
           statuspegawai 
           ON biodata.Nomor_Induk = statuspegawai.Nomor_Induk 
      where statuspegawai.bagian = 'IT' 
      Group By biodata.Nomor_Induk
     ) t;

您也可以在没有子查询的情况下表达这一点:

      select COUNT(distinct biodata.Nomor_Induk) as jumData
      from biodata INNER JOIN
           statuspegawai 
           ON biodata.Nomor_Induk = statuspegawai.Nomor_Induk 
      where statuspegawai.bagian = 'IT';

答案 1 :(得分:1)

请在此处使用以下代码:
-missing table别名!

select count(*) FROM (
 SELECT COUNT(biodata.Nomor_Induk) as jumData
 from biodata INNER JOIN  
 statuspegawai 
 ON biodata.Nomor_Induk =  statuspegawai.Nomor_Induk 
 where statuspegawai.bagian='IT' 
 Group By biodata.Nomor_Induk
 ) as Alias1;