sql - 将 NULL 替换为连接函数 SQL 中的自定义文本
问题描述
我想把属于同一个id的输出拼接起来,所以我用了下面的函数,但是我也想用自定义文本替换空值,怎么做才对呢?
GROUP_CONCAT(IFNULL(ps_state.name, 'No-State') SEPARATOR ', ')
如果需要,所有代码:
SELECT DISTINCT ps_customer.id_customer AS CustomersId, ps_customer.email AS CustomersName, GROUP_CONCAT(IFNULL(ps_state.name, 'No') SEPARATOR ', ') AS StatesName,
GROUP_CONCAT(ps_country_lang.name SEPARATOR ', ') AS CountryName
FROM ps_customer
INNER JOIN ps_address ON ps_customer.id_customer=ps_address.id_customer
LEFT JOIN ps_state ON ps_state.id_state=ps_address.id_state
INNER JOIN ps_country_lang ON ps_country_lang.id_country=ps_address.id_country
WHERE ps_country_lang.id_lang=1
GROUP BY ps_customer.id_customer, ps_state.name
解决方案
Your code basically looks right but I would use coalesce()
:
GROUP_CONCAT(coalesce(ps_state.name, 'No-State') SEPARATOR ', ')
You might also want to order within the GROUP_CONCAT()
, if there is an ordering that you want or expect.
推荐阅读
- java - 由于 RunTimeException,RecyclerView Espresso 测试失败
- jquery - 'body' 的 jQuery Click 事件在 iOS 上不起作用
- android - RecyclerView 上的快速滚动
- node.js - 在水平扩展的云环境中使用节点的“集群”有意义吗?
- security - Rop 缓冲区溢出
- reactjs - 是否可以直接从它的低阶组件将数据传递给 withTracker ?
- typescript - 如何从 Typescript 中的函数获取参数类型
- ionic-framework - admob免费插件的离子构建问题
- python - 将所有这些二进制列组合成分类列的更简单方法?
- node.js - 如何使用带有 Node.js 的 batchWriteItem 来查询 AWS DynamoDB?