【问题标题】:How can I apply Logical Operator to 2 Select statement? [duplicate]如何将逻辑运算符应用于 2 Select 语句? [复制]
【发布时间】:2021-04-14 15:08:29
【问题描述】:

我有 4 个名为 A, B, C, D 的表。我有2 conditons to get the data

Condition 1

Select *
From A, B, C
Where //All 3 tables are joined with conditions
limit 1; // Display 1 data at a time

Condition 2

Select *
From A, B, D
Where //All 3 tables are joined with conditions
limit 1; // Display 1 data at a time

我是什么looking for is to combine above 2 Select Statement 这样

If Data Exists in Condition 1 然后Display Data Display Data from condition 2

如何实现上述功能?

【问题讨论】:

  • mysqlpostgresql 是不同的数据库,你为什么要同时标记两个? (选择一个。)
  • 在第二个查询中执行 WHERE NOT EXISTS()....并将查询 1 合并到新查询 2
  • @Cato 好的,让我试试。我不能简单地使用OR吗?

标签: mysql sql select pgadmin


【解决方案1】:

你可以这样做

if exists(/*First query*/)
begin

/*FirstQuery*/

end

else

begin

/*Second Query*/

end

【讨论】:

  • 如何在js file中使用上述方法?
【解决方案2】:

我在想

Select *
From A, B, C
Where //All 3 tables are joined with conditions
limit 1 // Display 1 data at a time
UNION
Select *
From A, B, D
Where not exists(Select *
From A, B, C
Where //All 3 tables are joined with conditions)
limit 1

【讨论】:

  • 这不会像在2nd where clause 中那样提供所需的输出,我们没有定义By what conditions we need data in 2nd WHERE CLAUSE
  • 我想说的有意义吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 2017-01-01
  • 1970-01-01
  • 2019-05-08
  • 2015-12-12
  • 2017-10-10
  • 2013-11-25
相关资源
最近更新 更多