在 MariaDB 中按 null 分组
Group by null in MariaDB
我有一个 table 数据如下。
** ID TYP NAME END**
1 *null* ADEPLAST S.R.L.
2 *null* ADEPLAST S.A.
3 S.C. ADEPLAST S.R.L.
4 A.B. ADEPLAST S.R.L.
MariaDB 中有没有办法对其进行分组,以便我得到如下结果:
** ID TYP NAME END**
1,3 S.C. ADEPLAST S.R.L.
2 *null* ADEPLAST S.A.
4 A.B. ADEPLAST S.R.L.
我已经尝试过经典的 GROUP BY,但是正如我在某处读到的那样,NULL 值本身就是一个值,因此不会起作用
谢谢:)
SELECT
GROUP_CONCAT(ID) as `ID`,
`TYP`,
`NAME`,
`END`
FROM (
-- this will find the TYP when it is NULL, and the same name with
-- a TYPE exists
SELECT
ID,
CASE WHEN `TYP` IS NULL THEN (SELECT TYP
FROM table1 t1
WHERE t1.name=t0.name
and t1.`END`=t0.`END`
and NOT t1.TYP IS NULL
LIMIT 1)
ELSE TYP END as `TYP`,
Name,
`END`
FROM table1 t0
) x
GROUP BY `TYP`,`Name`,`END`
ORDER BY `ID`
我有一个 table 数据如下。
** ID TYP NAME END** 1 *null* ADEPLAST S.R.L. 2 *null* ADEPLAST S.A. 3 S.C. ADEPLAST S.R.L. 4 A.B. ADEPLAST S.R.L.
MariaDB 中有没有办法对其进行分组,以便我得到如下结果:
** ID TYP NAME END** 1,3 S.C. ADEPLAST S.R.L. 2 *null* ADEPLAST S.A. 4 A.B. ADEPLAST S.R.L.
我已经尝试过经典的 GROUP BY,但是正如我在某处读到的那样,NULL 值本身就是一个值,因此不会起作用
谢谢:)
SELECT
GROUP_CONCAT(ID) as `ID`,
`TYP`,
`NAME`,
`END`
FROM (
-- this will find the TYP when it is NULL, and the same name with
-- a TYPE exists
SELECT
ID,
CASE WHEN `TYP` IS NULL THEN (SELECT TYP
FROM table1 t1
WHERE t1.name=t0.name
and t1.`END`=t0.`END`
and NOT t1.TYP IS NULL
LIMIT 1)
ELSE TYP END as `TYP`,
Name,
`END`
FROM table1 t0
) x
GROUP BY `TYP`,`Name`,`END`
ORDER BY `ID`