mysql - 如何为 ```GROUP BY``` 列返回最高 ID NON NULL 字段?
问题描述
SELECT email, first_name, last_name from person group_by email order by id desc
如果 first_name 混合了值和空值,我将如何获得该列的最新 NOT NULL 值?
id | email | first_name | last_name
1 | a@b.com | ted | smith
2 | a@b.com | zed | smith
3 | a@b.com | NULL | johnson
我基本上想获得 first_name 的值 zed。
解决方案
假设您有一个person_id
具有排序的列,您可以简单地执行以下操作:
select p.*
from person p
where (email, person_id) in (select p2.email, max(p2.person_id)
from person p2
where p2.first_name is not null
group by p2.email
);
推荐阅读
- bash - 在远程服务器中跨 bash 会话更改 env 值
- java - 继承 ArrayList 给子类
- oracle - 变更集要求以识别由 liquibase 执行的唯一变更
- google-chrome - Chrome 扩展错误“runtime.lastError:消息端口在收到响应之前关闭。”
- java - executor.execute 方法的单元测试
- azure - 使用 Azure MSAL 浏览器 CDN js 初始化 msal 实例
- python - Python 串行排序
- rdf - 如何将三重文本文件转换为 RDF 格式
- android - 如何在颤振中测试 tensorflow lite 模型上的自定义录制音频
- java - (如果基于语句的分配)为什么这不起作用?