Mysql如果空结果返回默认值(Mysql If Empty Result Return Default Value)
如果初始条目不存在,我试图让mysql返回一组基本值。
这就是我的尝试:
SELECT
IFNULL(count( mcr.rating ), 1) as rating_count,
IFNULL(avg( mcr.rating ), 5) as rating_average,
IFNULL(sum( mcr.rating ), 5) as rating_value,
IFNULL(mck.slug, '-') as rating_slug,
IFNULL(mck.name, '-') as rating_name,
IFNULL(mck.criteria_id, '-') as id,
IFNULL(mck.category, '-') as category
FROM microstock_criteria_key AS mck
LEFT JOIN microstock_collective_reviews AS mcr ON mck.criteria_id = mcr.criteria_id
WHERE mcr.agency_id =5
GROUP BY mck.criteria_id
ORDER BY mck.priority ASC
但我仍然得到:
MySQL返回一个空结果集(即零行)。 (查询耗时0.0004秒)
当我应该(如果我理解)获得我在IFNULL语句中设置的默认值。
那么如何才能正确地完成返回默认结果?
I'm attempting to make mysql return a basic set of values if initial entries do not exist.
This is what I've attempted:
SELECT
IFNULL(count( mcr.rating ), 1) as rating_count,
IFNULL(avg( mcr.rating ), 5) as rating_average,
IFNULL(sum( mcr.rating ), 5) as rating_value,
IFNULL(mck.slug, '-') as rating_slug,
IFNULL(mck.name, '-') as rating_name,
IFNULL(mck.criteria_id, '-') as id,
IFNULL(mck.category, '-') as category
FROM microstock_criteria_key AS mck
LEFT JOIN microstock_collective_reviews AS mcr ON mck.criteria_id = mcr.criteria_id
WHERE mcr.agency_id =5
GROUP BY mck.criteria_id
ORDER BY mck.priority ASC
Yet I still get:
MySQL returned an empty result set (i.e. zero rows). (Query took 0.0004 sec)
When I should (if I understand) be getting the defaults I set in the IFNULL statements.
So how can this be done properly to return default result