如何在我的 sql 请求中添加条件?

How can I add conditions in my sql request?

我有一个关于 mysql 请求的问题。

我有以下两个 table 名字:

PROFILE
PROFILEXCEPTION

在我的请求中,我想第一时间搜索 table PROFILE 中是否存在我的设备名称,如果是我 return 结果,否则我在第二个搜索 table PROFILEXCEPTION.

如何在一个请求中完成。

这是从 table profile:

获取数据的查询
select last_name 
from profile
where first_name = 'John';

这是从 table profileexception 中获取数据的查询,如果它不存在于 table profile:

select last_name 
from profilexception
where first_name = 'John'
and not exists
(
  select *
  from profile
  where first_name = 'John'
);

所以第二个查询只会产生输出,如果第一个没有。

UNION ALL 粘在一起:

select last_name 
from profile
where first_name = 'John'
union all
select last_name 
from profilexception
where first_name = 'John'
and not exists
(
  select *
  from profile
  where first_name = 'John'
);