mysql 计数不返回零
我有这样的查询:
SELECT COUNT( tbl_workstation.workstation_id ) AS AVAILABLE, tbl_lab.room_no,
tbl_lab.capacity
FROM tbl_workstation, tbl_lab
WHERE tbl_workstation.logged_on =0
AND tbl_workstation.startup =1
AND tbl_workstation.lab_name = tbl_lab.lab_name
GROUP BY tbl_lab.lab_name
我得到了每个实验室的正确计数 - 但是当计数为 0 时 - 没有返回任何内容。我还需要返回计数为零的行。我已经考虑过进行左连接,但出现了各种各样的错误。从错误的结果集到不明确的列错误。我做错了什么?
I have this query:
SELECT COUNT( tbl_workstation.workstation_id ) AS AVAILABLE, tbl_lab.room_no,
tbl_lab.capacity
FROM tbl_workstation, tbl_lab
WHERE tbl_workstation.logged_on =0
AND tbl_workstation.startup =1
AND tbl_workstation.lab_name = tbl_lab.lab_name
GROUP BY tbl_lab.lab_name
I get the correct counts for each of the labs - However where the count is 0 - Nothing is returned. I need to return the rows where the count is zero also. I have looked at doing a left join but I get all sorts of errors. Anything from wrong result sets to ambiguous column errors. What am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想要获取所有实验室,即使是那些具有 0 个工作站的实验室,请使用 labs 表作为主表并外部连接到工作站表。
像这样的东西应该有效:
If you want to get all labs, even those with 0 workstations, use the labs table as your primary table and outer join to the workstations table.
Something like this should work:
问题出在您的
AND tbl_workstation.lab_name = tbl_lab.lab_name
上。此查询仅返回实验室中有工作站的结果,这就是为什么您不会从空实验室获得任何结果。你是对的,你需要使用
LEFT JOIN
这应该很好。
The problem lies in your
AND tbl_workstation.lab_name = tbl_lab.lab_name
. This query only return resultat where the lab has workstations in it, that's why you don't get any results from an empty lab.And you are right, you need to use a
LEFT JOIN
This should be good.
我建议这样:
我假设 tbl_workstation.startup 意味着工作站已打开且可用,并且未启动的工作站不可用。即工作站不得登录并且必须启动才能使用。
I would suggest something like this:
I assume that tbl_workstation.startup means that the workstation is turned on and available and that workstations that are not started up are not available. I.e. a workstation must not be logged on and must be started up to be available.