MySQL GROUP BY NULL 和 EMPTY

发布于 2024-11-18 22:59:24 字数 161 浏览 3 评论 0原文

在 MySQL 查询中,我使用文本字段执行GROUP BY。由于原始数据的性质,某些行包含此字段的空字符串,而其他行则为 true null

分组时,如何将空字符串和 null 分组在一起,将两者视为 null

In a MySQL query I am doing a GROUP BY with a text field. Due to the nature of the original data, some rows are contain empty strings for this field, and others are true null.

When grouping, how can I group empty string and null together, treating both as null?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

青衫儰鉨ミ守葔 2024-11-25 22:59:24

这可以通过 SELECT CASE 来完成。可能有一种我不知道的更简单的方法。

SELECT CASE 的格式是

SELECT
CASE
    WHEN table_name.text_field IS NULL OR table_name.text_field = ''
    THEN null
    ELSE table.text_field
END as new_field_name,
other_field, another_field, ...rest of query...

所以你看,你可以用 WHEN/THEN 将值 CASE 在一起,并用 ELSE 默认为实际值。

This can be accomplished by SELECT CASE. There may be a simpler way I do not know of.

The format of SELECT CASE is

SELECT
CASE
    WHEN table_name.text_field IS NULL OR table_name.text_field = ''
    THEN null
    ELSE table.text_field
END as new_field_name,
other_field, another_field, ...rest of query...

So you see, you can CASE together values with WHEN/THEN and default to the real value by ELSE.

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