sql - 如果选择为空,则查询以选择默认值
问题描述
这是一个简单的问题,但我在使其在 Access 中工作时遇到了严重的问题。
我有一个查询要做:
Select * from Rates where client="Jhonsons"
但是如果它不存在 Jhonsons 作为客户端,它需要选择一个通用的,运行这个查询:
Select * from Rates where client="General"
这就对了。这似乎是一项简单的任务,但给我带来了很大的挑战,我希望能得到一些帮助。
谢谢你的帮助!
解决方案
您可以使用top
和order by
:
select top 1 r.*
from Rates as r
where client in ("Jhonsons", "General")
order by iif(client = "General", 2, 1)
还有其他方法,例如:
select r.*
from Rates as r
where r.client = "Jhonsons" union all
select r.*
from Rates as r
where r.client = "General" and
not exists (select 1 from Rates as r2 where r2.client = "Jhonsons") ;
推荐阅读
- apache-spark - Spark array_contains 但用于 Set 类型
- javascript - Three.js 不加载 gltf 文件
- python - 如何在 django 中使用 ForeignKey 加入多个表
- python - 如何在python中的数据框列上使用正则表达式进行分组?
- mysql - Mysql 不会为复数搜索返回单数记录
- sql-server - Excel 删除我的 PowerQuery 数据源权限凭据?
- ffmpeg - ffmpeg中的纯白HDR视频
- python - DistutilsArgError:未提供命令
- rust - `#[lang = "..."]` 属性有什么作用?
- python - 在枚举列表中过滤枚举 sqlalchemy