mysql - 如何在 sql 请求中添加条件?

标签 mysql sql database phpmyadmin bdd

请问我有一个关于 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/

相关文章:

PHP PDO MySQL 不输出结果但加载其余页面?

SQL Server : REPLACE function replace ' ' with ',' in string

java - 如何提高 Java 数据库更新的性能?

python - Facebook 将不正确的数据传递给数据库

mysql - 左 JOIN 语法错误

php - 如何匹配与一组特定的其他记录关联的记录?

mysql - 选择具有固定奖杯数和积分的球员

sql - 为列中的每一行生成 guid

sql - SQL中如何设置参数来显示一段时间的数据?

database - 什么是表格表达式语句?