我有这些桌子:
任务组表
id name 1 cooking 2 baking
任务状态表
id name 1 open 2 done? 3 completed 4 re-open 5 suspend
任务表
project_id task_group_id task_status_id task_name 1 1 1 cooking martabak 1 1 2 cooking sate kambing 1 1 3 cooking nasi uduk 1 1 4 cooking nasi goreng 1 1 1 cooking martabak telor 1 1 2 cooking sate ayam 1 1 5 cooking nasi tutug oncom 1 1 5 cooking nasi pera 1 2 1 bake nastar 1 2 2 bake bolu pisang 1 2 3 bake bika ambon 1 2 5 bake bolu kukus 1 2 3 bake cheese stik 1 2 1 bake bolu ketan 1 2 5 bake papais 1 2 1 bake boled
我想要的结果是这样的:
project task_group task_status number Restaurant Cooking Completed 1 Restaurant Cooking Done? 2 Restaurant Cooking Open 2 Restaurant Cooking Re-open 1 Restaurant Baking Completed 2 Restaurant Baking Done? 1 Restaurant Baking Open 3 Restaurant Baking Re-open 0
排除任务状态“挂起”
我已尝试使用where子句task_status.name的子查询将tasks表与task status表交叉联接在('open','done?',但无法获取正确的计数聚合

最佳答案

我想你可以这样做:

SELECT
    project.name AS project,
    Task_Group.name as task_group ,
    Task_Status.name as task_status,
    COUNT(*) AS number
FROM
    Tasks
    JOIN Task_Group
        ON Tasks.task_group_id = Task_Group.id
    JOIN Task_Status
        ON Task.task_status_id=Task_Status.id
    JOIN project_table
        ON Task.project_id=project_table.project_id
WHERE
    Task_Status.Id IN(1,2,3,4)
GROUP BY
    project_table.name,
    Task_Group.name,
    Task_Status.name

关于mysql - 交叉联接左联接计数行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44384540/

10-11 00:41