请问我有一个关于 mysql 请求的问题。
我有以下两个表,名称为:
PROFILE
PROFILEXCEPTION
我的请求是,我想第一时间查询表PROFILE
中是否存在我的设备名称。 ,如果是,我返回结果,否则我在第二个表 PROFILEXCEPTION
中搜索.
我怎样才能在一个请求中完成它。
最佳答案
这是从表profile
中获取数据的查询:
select last_name
from profile
where first_name = 'John';
这是从表 profileexception
中获取数据(如果表 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'
);
关于mysql - 如何在 sql 请求中添加条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32627202/