我需要编写一个返回公司名称的查询,以及公司拥有的特定工作订单的数量。
现在我的查询是这样的:
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
有人可以帮我这个吗?
由于
答案 0 :(得分: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
答案 1 :(得分: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:
答案 2 :(得分: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