首页 > 解决方案 > 如果选择为空,则查询以选择默认值

问题描述

这是一个简单的问题,但我在使其在 Access 中工作时遇到了严重的问题。

我有一个查询要做:

Select * from Rates where client="Jhonsons"

但是如果它不存在 Jhonsons 作为客户端,它需要选择一个通用的,运行这个查询:

Select * from Rates where client="General"

这就对了。这似乎是一项简单的任务,但给我带来了很大的挑战,我希望能得到一些帮助。

谢谢你的帮助!

标签: sqlms-access

解决方案


您可以使用toporder 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") ;

推荐阅读