sql - 获取 sql 结果中的预定义记录,而不管它是否存在
问题描述
我有一个表,其中员工姓名中有重复值,但描述是唯一的。我只想选择我在脚本中预定义了名称的 5 名员工。
select
Employee in (John A, Ronny C, Harry D, David R, Alison R)
from Employee table
where employee in (John A, Ronny C, Harry D, David R, Alison R) and description like "% Manager %"
在上面的脚本中,如果 Johan A 不是经理,那么他的记录将不会出现,我希望他的名字出现在结果中,但描述栏中应该有“NULL”
解决方案
如果我理解正确,您可以使用聚合:
select name,
max(case when description like '%Manager%' then description end) as manager_description
from employee
where name in ('John A', 'Ronny C', 'Harry D', 'David R', 'Alison R')
group by name;
这将返回表中所有匹配的名称。如果有“经理”描述,则返回该描述。如果没有,那么NULL
。
推荐阅读
- javascript - 边缘浏览器需要刷新页面才能在 aurelia 框架中使用 javascript 查看上传的文件
- angular - 如何列出表中不完整的记录列表?
- android - Android 模拟器不适用于 windows 10 pro 1803
- java - 托管在 Godaddy 中时 Java 应用程序处理速度很慢
- git - 如何解决 git 存储冲突?
- javascript - 在laravel中使用javascript将图像上传到数据库
- java - 我想要一个从java代码调用servlet的代码。我的代码如下。Servlet 没有被调用,
- oracle - 在不知道表名的情况下将变量声明为行类型时需要帮助
- python - 网页抓取图像(美丽的汤)到具有一个功能的目录中,试图将这些图像附加到另一个功能的列表中,挣扎
- elasticsearch - 混合睡眠模式下的弹性搜索 - 嵌入到应用程序中