我有三个表,A、B 和C。它们都包含不同的数据,但有一些共同的列。
如果A、B和C都有列C1和C2 那么我如何使用可能在 3 个表中的任何一个中的 C1 值查找特定的 C2 值?
基本上,我想做一个简单的查找,但让它作用于 3 个表的联合 - 我宁愿不使用 View 来实现这一点。
请注意,这是一个 Ingres Vectorwise 数据库。
最佳答案
您可以通过合并 from
子句中的表来做到这一点:
select c2
from ((select c1, c2 from a) union all
(select c1, c2 from b) union all
(select c1, c2 from c)
) t
where c1 = <your value>
出于性能原因,我使用了union all
。如果您担心重复值,请使用 union
或在 select
中添加 distinct
。
这是标准的 SQL,应该适用于任何数据库。
关于sql - 从三个表的相同列的联合中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13075090/