数据库查询 – 无法构建查询

huangapple go评论102阅读模式
英文:

Database query - not able to frame it

问题

让我在这里解释一下问题陈述:

我需要从两个表中提取数据。根据用户输入,在后台查询应首先检查第一个表,如果数据可用,应返回并退出。如果数据不可用,应执行第二个查询并返回结果。有人可以帮助我示范如何在Informix中使用if-else或CASE语句吗?

英文:

Let me explain the problem statement here:

I have to pull data from 2 tables. Based on the user input, in the backend the query should first check the first table, if data is available should return and exit. If data is not available, should execute the seconds query and return the result. Can you someone help me give an example how to use if-else or CASE statement for informix?

答案1

得分: 1

如果两个查询返回相同的列,您可以这样表示:

select t1.*
from table1 t1
union all
select t2.*
from table2 t2
where not exists (select 1 from table1);

如果需要从这两个查询中获取的列不同,那么您应该在应用程序级别处理这个情况 - 执行第一个查询,如果没有返回结果,则执行第二个查询。实际上,这可能更可取,因为应用程序中的逻辑更清晰。

英文:

If the two queries return the same columns, you can express this as:

select t1.*
from table1 t1
union all
select t2.*
from table2 t2
where not exists (select 1 from table1);

If the columns needed from the two queries are not the same, then you should handle this at the application level -- run the first query and if nothing is returned, run the second query. That might actually be preferable anyway, because the logic is clearer in the application.

huangapple
  • 本文由 发表于 2020年7月24日 16:40:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63069999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定