Mysql GROUP BY excludes null data

When using  group bya certain column name for grouping statistics, some of the data of the column name is null, so the null data rows will be divided into one group, resulting in data errors, so the data rows of the null column name cannot be executed group by

直接执行查询得到的结果为:

使用group by进行查询得到的结果为:

Obviously, because some columns of master_name are empty, all the empty master_names are merged into one group when grouping.

solution:

IFNULL() function and UUID() function

There are similar solutions on the Internet, which can be solved by IFNULL()matching UUID()functions with functions.

1. IFNULL() function

IFNULL() The function is used to determine whether the first expression is NULL, if it is NULL, it returns the value of the second parameter, if it is not NULL, it returns the value of the first parameter.

The format is as follows:

IFNULL(expression, alt_value)
-- 即可以是列名
IFNULL(user.id, "hahha")
-- 也可以是具体的值
IFNULL(null, "hahha")

2. UUID() function

UUID()Functions can generate unique values ​​in time.

3. Combined use

Therefore,  group by the format of using these two functions later can be as follows

group by IFNULL('列名', UUID())

The sql that got the result correctly is:

Guess you like

Origin blog.csdn.net/Crystalqy/article/details/114086528