从mysql中的表中计算多列

时间:2015-02-18 10:09:22

标签: mysql join count

我总共有3张桌子

tbl_projectstbl_bugtbl_bug_history

我需要为每个项目显示总共3个计数。

1.每个项目的总错误 - 来自tbl_bug

  1. "无效"的总计数,"重复的总数" - 这是来自错误历史记录
  2. 输出应采用以下样本格式


    项目名称|总bug |无效|重复|


    项目一| 5 | 6 | 7 |

    请帮我表结构定义如下

    CREATE TABLE IF NOT EXISTS `tbl_bug` ( 
    `id` int(10) NOT NULL AUTO_INCREMENT, 
    `project_id` int(10) NOT NULL, 
    `bugname` varchar(250) NOT NULL, 
    PRIMARY KEY (`id`) 
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ; 
    

    转储表tbl_bug

    的数据
    INSERT INTO `tbl_bug` (`id`, `project_id`, `bugname`) VALUES 
    (1, 1, 'first-bug'), 
    (2, 1, 'second-bug'), 
    (3, 1, 'bug-third'), 
    (4, 1, 'bug-four'), 
    (5, 1, 'bug-give'), 
    (6, 1, 'master-bug'), 
    (7, 2, 'error-notice'), 
    (8, 3, 'invalid bug'), 
    (9, 4, 'insufficinet memory'), 
    (10, 4, 'hello bug'); 
    
    CREATE TABLE IF NOT EXISTS `tbl_bug_history` ( 
    `id` int(10) NOT NULL AUTO_INCREMENT, 
    `bug_id` int(10) NOT NULL, 
    `status` varchar(100) NOT NULL, 
    PRIMARY KEY (`id`) 
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ; 
    

    转储表tbl_bug_history

    的数据
    INSERT INTO `tbl_bug_history` (`id`, `bug_id`, `status`) VALUES 
    (2, 1, 'invalid'), 
    (3, 2, 'invalid'), 
    (6, 3, 'duplicate'), 
    (7, 4, 'feedback'), 
    (10, 5, 'duplicate'), 
    (11, 6, 'duplicate'), 
    (12, 6, 'invalid'), 
    (13, 7, 'feedback'), 
    (14, 7, 'normal'), 
    (15, 8, 'duplicate'), 
    (16, 8, 'normal'), 
    (18, 9, 'feedback'), 
    (19, 10, 'invalid'), 
    (20, 10, 'feedback'); 
    
    
    
    CREATE TABLE IF NOT EXISTS `tbl_projects` ( 
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `projectname` varchar(250) NOT NULL, 
    PRIMARY KEY (`id`) 
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; 
    

    转储表tbl_projects

    的数据
    INSERT INTO `tbl_projects` (`id`, `projectname`) VALUES 
    (1, 'project-one'), 
    (2, 'project-two'), 
    (3, 'project-three'), 
    (4, 'project-four');
    

1 个答案:

答案 0 :(得分:0)

试试这个。这是你的预期输出。

http://sqlfiddle.com/#!2/1d756/3