我正在尝试创建一个显示来自不同表的一些信息的SQL查询。但我得到错误Subquery返回多行SQL。我希望它显示多行。
SELECT c.Name,
jn.ID,
jn.ActualWeight as GrossWt,
jn.JobNo,
COUNT(distinct jn.JobNo) as Jobs,
COUNT(distinct jd.JobID) as Dbriefs,
COUNT(distinct jn.OutTurn) as Outturns,
(select Status from jobstat where CompanyID = jn.CompanyID AND Status = "DEL") as Delivery
FROM job_new jn
LEFT JOIN customer c ON jn.CompanyID = c.Company_ID
LEFT JOIN job_debriefs jd ON jn.JobNo = jd.JobID
LEFT JOIN jobstat js ON jn.CompanyID = js.CompanyID
WHERE jn.CompanyID = 36
我尝试过添加GROUP BY和ORDER BY,但这也不起作用。如果我删除了选择状态....行,它只显示一行,当它应显示超过一百
答案 0 :(得分:0)
您需要group by
:
SELECT c.Name, jn.ID, jn.ActualWeight as GrossWt, jn.JobNo,
COUNT(distinct jn.JobNo) as Jobs,
COUNT(distinct jd.JobID) as Dbriefs,
COUNT(distinct jn.OutTurn) as Outturns,
jobstat
FROM job_new jn LEFT JOIN
customer c
ON jn.CompanyID = c.Company_ID LEFT JOIN
job_debriefs jd
ON jn.JobNo = jd.JobID LEFT JOIN
jobstat js
ON jn.CompanyID = js.CompanyID
WHERE jn.CompanyID = 36
GROUP BY c.Name, jn.ID, jn.ActualWeight as GrossWt, jn.JobNo, js.status
我不确定子查询应该做什么,所以我猜是关于js.status
。
原始查询的问题是在COUNT()
中使用了SELECT
。这会将查询转换为聚合查询。如果没有GROUP BY
,则只返回一行。在大多数其他数据库中,您通常会收到错误。