Sql 选择唯一结果并为空
我需要从表中获取在某些字段和所有行中具有唯一值的所有行,而不是在此字段中具有Sql 选择唯一结果并为空,sql,Sql,我需要从表中获取在某些字段和所有行中具有唯一值的所有行,而不是在此字段中具有null。例如: id | name | group ----------------- 1 | One | 1 2 | Two | null 3 | Three| 3 4 | Four | 2 5 | Five | 1 6 | Six | 2 7 | Seven| null 结果: id | name | group ----------------- 1 | One | 1 2 | Two
null
。例如:
id | name | group
-----------------
1 | One | 1
2 | Two | null
3 | Three| 3
4 | Four | 2
5 | Five | 1
6 | Six | 2
7 | Seven| null
结果:
id | name | group
-----------------
1 | One | 1
2 | Two | null
3 | Three| 3
4 | Four | 2
7 | Seven| null
如何在一个请求中完成它?选择“四”而不是“六”的逻辑是什么?您确实需要加入
JOIN
select t.id, t.name, t.`group`
from tablename t
join (select `group`, min(id) as mid
from tablename
where `group` is not null
group by `group`) x on x.mid = t.id and x.`group` = t.`group`
union all
select id, name, `group`
from tablename
where `group` is null