mysql ifempty_Mysql如果空结果返回默认值(Mysql If Empty Result Return Default Value)

博主在尝试从MySQL查询时,希望当结果为空时返回预设的默认值,使用了IFNULL函数,但仍然得到空结果。问题在于JOIN操作,通过调整为RIGHT JOIN解决了问题,确保即使没有匹配项也能返回默认值。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值