mysql - 如果在另一个表中找到记录,则从表中选择

标签 mysql sql oracle oracle11g

我需要从表 1 中选择一些行假设是否在表 2 中找到了一个值。所以我想检查是否在表 2 中找到该值(我将从命令行输入值)然后选择行来自 Table1,如果不是,我想从另一个表中选择行。 我试过 CASE 但从我得到的结果来看,只有当你想检查一个表中的值时才有效。有什么想法吗?

最佳答案

你可以这样做:

-- If value is found in table2, select from table1
select * -- <- use padding if necessary 
  from table1
 where exists (select 1
                 from table2
                where myField = value)

union all

-- If value is not found in table2, select from another_Table
select * -- <- use padding if necessary
  from another_Table
 where not exists (select 1
                     from table2
                    where myField = value)

关于mysql - 如果在另一个表中找到记录,则从表中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18310838/

相关文章:

mysql - 将列名映射到字段名 LOAD XML LOCAL INFILE

php - 如何输出正确的数据库行

sql - 使用 nvl 和 max 查找唯一值

oracle - 撤销 session 和帐户锁定有什么区别?

mysql - SQL 列表项不重复第一个

java - 从 List<Object[]> 构建树关系图的有效方法

php - Mysql数据库整理与捷克符号

php - 尝试比较两个日期...一个来自存储为 DATE 的 mysql DB,另一个使用 DateTime 对象

mysql - 每个用户返回 2 行

sql - 按分组列值的变化对数据进行分组