0

我需要编写一个返回公司名称的查询,以及该公司拥有的特定工作订单的数量。

现在我的查询是这样的:

SELECT c.name, cj.joborder_id
FROM company c, joborder jo, candidate_joborder cj
WHERE c.company_id=jo.company_id
AND jo.joborder_id=cj.joborder_id
AND jo.status = 'Active'
AND cj.status=700;

这将返回下表:

Name | Job Order ID
X    | 1874
Y    | 2003
Y    | 2003
Z    | 2001

我想要的是:

Name | Count
X    | 1
Y    | 2
Z    | 1

有人可以帮我弄这个吗?

谢谢

4

3 回答 3

2

使用COUNT()GROUP BY从句,

SELECT c.name, COUNT(cj.joborder_id) TotalCount
FROM   company c, joborder jo, candidate_joborder cj
WHERE  c.company_id=jo.company_id
       AND jo.joborder_id=cj.joborder_id
       AND jo.status = 'Active'
       AND cj.status=700
GROUP BY c.name

使用ANSI JOIN

SELECT  c.name, 
        COUNT(cj.joborder_id) TotalCount
FROM    company c
        INNER JOIN joborder jo
            ON c.company_id = jo.company_id
        INNER JOIN candidate_joborder cj
            ON jo.joborder_id = cj.joborder_id
WHERE   jo.status = 'Active' AND 
        cj.status=700
GROUP   BY c.name
于 2013-05-21T15:36:21.450 回答
2

您想要的查询如下:

SELECT c.name,
       count(cj.joborder_id)
FROM company c,
     joborder jo,
     candidate_joborder cj
WHERE c.company_id=jo.company_id
  AND jo.joborder_id=cj.joborder_id
  AND jo.status = 'Active'
  AND cj.status=700
GROUP BY c.name;

我建议以下参考用于 SQL 聚合,特别是 group by 和 count:

于 2013-05-21T15:36:25.330 回答
1
SELECT DISTINCT(c.name), COUNT(cj.joborder_id)
FROM company c, joborder jo, candidate_joborder cj
WHERE c.company_id=jo.company_id
AND jo.joborder_id=cj.joborder_id
AND jo.status = 'Active'
AND cj.status=700
GROUP BY c.name
于 2013-05-21T15:36:28.403 回答