mysql 查询在查询中显示为空
mysql query showing empty in query
我怎样才能像我想要这个输出一样查询,就像我按 tbl_clinics.clinic_name 分组一样。现在我的查询发生了什么?为什么会这样,它没有显示我的零值,就像我想要这样的输出:
clinic name | total_check
Clinic 1 | 4
Clinic 2 | 0
Clinic 3 | 0
Clinic 4 | 0
这是我的查询:
SELECT
tbl_clinics.clinic_name,
COALESCE(COUNT(tbl_check_up.check_up_id),0) AS total_check
FROM
tbl_clinics
LEFT OUTER JOIN tbl_check_up ON tbl_clinics.clinic_id = tbl_check_up.clinic_id
WHERE tbl_clinics.user_id = "102" AND MONTH(tbl_check_up.check_up_date) = "02"
GROUP BY tbl_clinics.clinic_name
而我得到的只有这些:
clinic_name | total_check
Clinic 1 | 4
为什么它没有显示我的其他诊所? ..我该如何操纵这些? .请帮助我从我想要的输出中得到正确的输出,并向我解释为什么我从我想要的输出中表现不佳。
WHERE 子句将确定将输出哪些行。如果你想输出 table "tbl_clinics" 内的所有记录,试试这个:
SELECT tbl_clinics.clinic_name,
Count(tbl_check_up.check_up_id) AS total_check
FROM tbl_clinics
LEFT JOIN tbl_check_up
ON tbl_clinics.clinic_id = tbl_check_up.clinic_id
AND tbl_clinics.user_id = "102"
AND Month(tbl_check_up.check_up_date) = "02"
GROUP BY tbl_clinics.clinic_name
此外,我认为 COUNT() 上的 COALESCE() 是不必要的,因为 COUNT() 将始终 return 0,而不是 NULL,当没有遇到任何行时。
你可以试试
SELECT tbl_clinics.clinic_name,
COALESCE(COUNT(tbl_check_up.check_up_id), 0) AS total_check
FROM tbl_clinics
LEFT OUTER JOIN tbl_check_up
ON tbl_clinics.clinic_id = tbl_check_up.clinic_id
WHERE tbl_clinics.user_id = "102"
AND (tbl_check_up.check_up_date IS NULL
OR MONTH(tbl_check_up.check_up_date) = "02")
GROUP BY tbl_clinics.clinic_name
我怎样才能像我想要这个输出一样查询,就像我按 tbl_clinics.clinic_name 分组一样。现在我的查询发生了什么?为什么会这样,它没有显示我的零值,就像我想要这样的输出:
clinic name | total_check
Clinic 1 | 4
Clinic 2 | 0
Clinic 3 | 0
Clinic 4 | 0
这是我的查询:
SELECT
tbl_clinics.clinic_name,
COALESCE(COUNT(tbl_check_up.check_up_id),0) AS total_check
FROM
tbl_clinics
LEFT OUTER JOIN tbl_check_up ON tbl_clinics.clinic_id = tbl_check_up.clinic_id
WHERE tbl_clinics.user_id = "102" AND MONTH(tbl_check_up.check_up_date) = "02"
GROUP BY tbl_clinics.clinic_name
而我得到的只有这些:
clinic_name | total_check
Clinic 1 | 4
为什么它没有显示我的其他诊所? ..我该如何操纵这些? .请帮助我从我想要的输出中得到正确的输出,并向我解释为什么我从我想要的输出中表现不佳。
WHERE 子句将确定将输出哪些行。如果你想输出 table "tbl_clinics" 内的所有记录,试试这个:
SELECT tbl_clinics.clinic_name,
Count(tbl_check_up.check_up_id) AS total_check
FROM tbl_clinics
LEFT JOIN tbl_check_up
ON tbl_clinics.clinic_id = tbl_check_up.clinic_id
AND tbl_clinics.user_id = "102"
AND Month(tbl_check_up.check_up_date) = "02"
GROUP BY tbl_clinics.clinic_name
此外,我认为 COUNT() 上的 COALESCE() 是不必要的,因为 COUNT() 将始终 return 0,而不是 NULL,当没有遇到任何行时。
你可以试试
SELECT tbl_clinics.clinic_name,
COALESCE(COUNT(tbl_check_up.check_up_id), 0) AS total_check
FROM tbl_clinics
LEFT OUTER JOIN tbl_check_up
ON tbl_clinics.clinic_id = tbl_check_up.clinic_id
WHERE tbl_clinics.user_id = "102"
AND (tbl_check_up.check_up_date IS NULL
OR MONTH(tbl_check_up.check_up_date) = "02")
GROUP BY tbl_clinics.clinic_name