MySql 带内连接的存储过程

MySql StoredProcedure with inner join

我有一个包含两列的 table 命名区域:region_id 是主键

       |region_id | region_code |
       | ----------+-------------+
       |         1 | Asia        |
       |         2 | Can         |
       |         3 | Cen         |
       |         4 | West        |
       |         5 | GNW         |
        ----------+-------------+

我还有另一个 table 员工,其中 emp_id 是主键,region_id_fk 是映射到区域 table:

的外键
     +------+---------+---------+------------+---------------+---------+------------
    |emp_id| emp_name|global_id|region_id_fk|attendance_date|ispresent| is_billable|
    +------+---------+---------+------------+---------------+---------+---------
    |  1   | andrew  | candrew |          1 | 2017-02-13    |      1  |          1 |
    |  2   | andrew  | candrew |          1 | 2017-02-14    |      1  |          1 |
    |  3   | andrew  | candrew |          1 | 2017-02-15    |      1  |          1 |
    |  4   | andrew  | candrew |          1 | 2017-02-16    |      1  |          1 |
    |  5   | andrew  | candrew |          1 | 2017-02-17    |      0  |          1 |
    |  6   | simon   | csimon  |          1 | 2017-02-13    |      1  |          1 |
    |  7   | simon   | csimon  |          1 | 2017-02-14    |      1  |          1 |
    |  8   | simon   | csimon  |          1 | 2017-02-15    |      1  |          1 |
    |  9   | simon   | csimon  |          1 | 2017-02-16    |      1  |          1 |
    |  10  | simon   | csimon  |          1 | 2017-02-17    |      1  |          1 |
    |  11  | peter   | cpeter  |          2 | 2017-02-13    |      1  |          1 |
    |  12  | peter   | cpeter  |          2 | 2017-02-14    |      1  |          1 |
    |  13  | peter   | cpeter  |          2 | 2017-02-15    |      1  |          1 |
    |  14  | peter   | cpeter  |          2 | 2017-02-16    |      1  |          1 |
    |  15  | alvin   | calvin  |          2 | 2017-03-13    |      1  |          0 |
    |  16  | thomas  | thomas  |          2 | 2017-03-14    |      0  |          1 |
    |  17  | samuel  | csamuel |          2 | 2017-03-15    |      1  |          0 |
    |  18  | jackson | cjackson|          2 | 2017-03-16    |      1  |          0 |
    |  19  | clinda  | clinda  |          2 | 2017-03-17    |      1  |          1 |
    +--------+----------+-----------+--------------+-----------------+------

我已经有了这种类型的查询,我在存储过程中使用了内部联接和计数:

DELIMITER $$
DROP PROCEDURE IF EXISTS test.some_proc $$
CREATE PROCEDURE test.some_proc(IN in_is_billable INT,IN in_month INT,IN in_ispresent INT)  
BEGIN  
      DECLARE capacitycount INT;
      DECLARE hrs INT;
      SET hrs=8;
      SELECT COUNT(ispresent)*8 AS team_capacity, region_code 
      FROM employee emp
      INNER JOIN  region r ON r.region_id=emp.region_id_fk
      WHERE (is_billable=in_is_billable) AND
      (MONTH(attendance_date)=in_month) AND   
      (ispresent=in_ispresent);
END $$
DELIMITER ;

它给了我以下结果: 团队能力|region_code 128 |亚洲

我希望我的存储过程给出区域 table 中存在的所有区域代码以及我在存储过程中计算的团队容量,这给出了正确答案。

结果应该是这样的,但目前我只获得了一个地区的团队容量。

 team capacity|region_code
    128          |Asia
    39           |Can
    68           |Cen   

任何帮助将不胜感激, 提前致谢:)

您在语法中错过了分组依据

DELIMITER $$
DROP PROCEDURE IF EXISTS test.some_proc $$
CREATE PROCEDURE test.some_proc(IN in_is_billable INT,IN in_month INT,IN in_ispresent INT)  
BEGIN  
       DECLARE capacitycount INT;
       DECLARE hrs INT;
       SET hrs=8;
       SELECT COUNT(ispresent)*8 AS team_capacity, region_code 
       FROM employee emp
       INNER JOIN  region r ON r.region_id=emp.region_id_fk
       WHERE (is_billable=in_is_billable) AND (MONTH(attendance_date)=in_month) AND   
      (ispresent=in_ispresent)
       group by r.region_code;
END $$
DELIMITER ;