mysql 计数不返回零

发布于 2024-11-15 00:08:15 字数 414 浏览 2 评论 0原文

我有这样的查询:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

和我恋爱吧 2024-11-22 00:08:15

如果您想要获取所有实验室,即使是那些具有 0 个工作站的实验室,请使用 labs 表作为主表并外部连接到工作站表。

像这样的东西应该有效:

SELECT COUNT( tbl_workstation.workstation_id ) AS AVAILABLE, tbl_lab.room_no,
tbl_lab.capacity
FROM tbl_lab
LEFT OUTER JOIN tbl_workstation ON 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

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:

SELECT COUNT( tbl_workstation.workstation_id ) AS AVAILABLE, tbl_lab.room_no,
tbl_lab.capacity
FROM tbl_lab
LEFT OUTER JOIN tbl_workstation ON 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
凉墨 2024-11-22 00:08:15

问题出在您的 AND tbl_workstation.lab_name = tbl_lab.lab_name 上。此查询仅返回实验室中有工作站的结果,这就是为什么您不会从空实验室获得任何结果。

你是对的,你需要使用 LEFT JOIN

SELECT COUNT( tbl_workstation.workstation_id ) AS AVAILABLE, tbl_lab.room_no,
tbl_lab.capacity
FROM tbl_workstation
LEFT OUTER JOIN tbl_lab ON bl_workstation.lab_name = tbl_lab.lab_name
WHERE tbl_workstation.logged_on =0
AND tbl_workstation.startup =1
GROUP BY tbl_lab.lab_name

这应该很好。

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

SELECT COUNT( tbl_workstation.workstation_id ) AS AVAILABLE, tbl_lab.room_no,
tbl_lab.capacity
FROM tbl_workstation
LEFT OUTER JOIN tbl_lab ON bl_workstation.lab_name = tbl_lab.lab_name
WHERE tbl_workstation.logged_on =0
AND tbl_workstation.startup =1
GROUP BY tbl_lab.lab_name

This should be good.

狠疯拽 2024-11-22 00:08:15

我建议这样:

SELECT
    tbl_lab.lab_name
    , tbl_lab.room_no
    , tbl_lab.capacity
    , SUM (
            CASE WHEN tbl_workstation.logged_on = 1 OR tbl_workstation.startup = 0 THEN 0 ELSE 1 END
    ) as AVAILABLE
FROM tbl_lab, tbl_workstation
WHERE
    tbl_workstation.lab_name = tbl_lab.lab_name
GROUP BY tbl_lab.lab_name, tbl_lab.room_no, tbl_lab.capacity
ORDER BY tbl_lab.lab_name, tbl_lab.room_no

我假设 tbl_workstation.startup 意味着工作站已打开且可用,并且未启动的工作站不可用。即工作站不得登录并且必须启动才能使用。

I would suggest something like this:

SELECT
    tbl_lab.lab_name
    , tbl_lab.room_no
    , tbl_lab.capacity
    , SUM (
            CASE WHEN tbl_workstation.logged_on = 1 OR tbl_workstation.startup = 0 THEN 0 ELSE 1 END
    ) as AVAILABLE
FROM tbl_lab, tbl_workstation
WHERE
    tbl_workstation.lab_name = tbl_lab.lab_name
GROUP BY tbl_lab.lab_name, tbl_lab.room_no, tbl_lab.capacity
ORDER BY tbl_lab.lab_name, tbl_lab.room_no

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文