两个SQL语句用于计算另一个表中的列

时间:2015-03-21 22:41:09

标签: mysql sql

我有一张表来存储有关项目的信息,例如项目名称和所有者等。

我有另一张表,其中包含许多与一个项目相关的任务。

如何运行查询以向我显示任务表中的所有打开任务?

这是我想要的结果集:

Project Name    Project Owner    Open Tasks
Test 1          Test Owner       5
Test 2          Test Owner       0

我的sql声明:

SELECT [Project Name] as Name1, [Project Owner]
FROM ProjectNames
UNION
SELECT distinct(count) 
FROM ActionItems
WHERE ProjectName = Name1
Order By Name1

1 个答案:

答案 0 :(得分:1)

SELECT
    pn.`Project Name`,
    pn.`project owner`,
    COUNT(ai.actions) AS `Open Tasks`
FROM ProjectNames AS pn
INNER JOIN ActionItems AS ai
    ON ai.`ProjectName` = pn.`Project Name`
GROUP BY pn.`Project Name`
ORDER BY `Open Tasks`

我认为这样可以正常工作。相应地更改列/表名称。