2

我有以下查询,它应该提供近 50 行作为输出。它正在从记录少于 10K 的表中搜索。运行查询大约需要 5 分钟

select a.dak_dept,b.dept_name,
  (
    select count(dak_stat) 
      from dak_dept_mast 
        where dak_dept = a.dak_dept and dak_stat='N'
  ) as new,
  (
    select count(dak_stat)
      from dak_dept_mast 
        where dak_dept = a.dak_dept and dak_stat='O'
  ) as open,
  (
    select count(dak_stat)
      from dak_dept_mast
        where dak_dept = a.dak_dept and dak_stat='C'
  ) as closed
  from dak_dept_mast a,tapal_dept_mast b
  where a.dak_dept = b.dept_code
  group by a.dak_dept

表 dak_dept_mast 将 dak_dept 和 dak_no 作为组合主键 表 tapal_dept_mast 将 dept_code 作为主键。

有人可以提出一些建议。我正在使用 XAMPP 运行 mysql 5.1

提前谢谢

苏吉什

4

1 回答 1

1

您可以尝试另一种选择

SELECT a.dak_dept,
       b.dept_name,
       SUM(CASE WHEN a.dak_stat='N' THEN 1 ELSE 0 END) new,
       SUM(CASE WHEN a.dak_stat='O' THEN 1 ELSE 0 END) open,
       SUM(CASE WHEN a.dak_stat='C' THEN 1 ELSE 0 END) closed
  FROM dak_dept_mast a JOIN tapal_dept_mast b
    ON a.dak_dept = b.dept_code
 GROUP BY a.dak_dept
于 2013-08-05T09:51:14.347 回答