查看包含相同列的多个表

发布于 2024-07-10 18:21:16 字数 112 浏览 4 评论 0原文

我有四个包含完全相同的列的表,并且想要创建所有四个表的视图,以便我可以一起查询它们。

这可能吗?

(由于繁琐的原因,我不能/不允许将它们组合起来,这将使这变得无关紧要!)

I have four tables containing exactly the same columns, and want to create a view over all four so I can query them together.

Is this possible?

(for tedious reasons I cannot/am not permitted to combine them, which would make this irrelevant!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

山人契 2024-07-17 18:21:16

假设除了具有相同的列名之外,相同的列还包含相同的数据,您希望创建一个作为所有这些表的并集的视图。

像下面这样的东西应该可以工作,但是我的 SQL 生锈了:

(CREATE VIEW view_name AS
(SELECT * FROM table1
UNION
SELECT * FROM table2
UNION
SELECT * FROM table3));

Assuming that in addition to having the same column names, columns of the same contain the same data, you want to create a view that is the union of all those tables.

Something like the following should work, but my SQL is rusty:

(CREATE VIEW view_name AS
(SELECT * FROM table1
UNION
SELECT * FROM table2
UNION
SELECT * FROM table3));
虐人心 2024-07-17 18:21:16

值得注意的是,您可能需要使用“union all”来保留可能存在于多个表中的唯一行。 标准联合将删除重复项。

It may be worth noting that you might need to use "union all" to preserve unique rows which may exist in more than one of the tables. A standard union will remove duplicates.

笙痞 2024-07-17 18:21:16

从您的查询中很难判断您是否希望数据基于 UNION 返回,或者作为包含离散列的视图返回。 这显然有效果。

考虑以下示例:

TableA
ID  Name   RelatedID
1   John   2
2   Paul   1

TableB
ID  Name   RelatedID
1   Ringo  1
2   George 1

TableC
ID  Name  RelatedID
1   Bob   1

TableD
ID  Name  RelatedID
1   Kate  NULL

现在,对此运行以下查询:

SELECT ID, Name FROM TableA
联合所有
从表 B 中选择 ID、名称
联合所有
从 TableC 中选择 ID、名称
联合所有
SELECT ID, Name FROM TableD

这会产生以下输出:

1 John
2 Paul
1 Ringo
2 George
1 Bob
1 Kate

这是您想要的吗? 如果是这样,您可以使用 UNION 查询。

现在,如果您想要的效果是获得相关数据的离散视图,您可能需要执行如下操作:

SELECT A.ID MasterID, A.Name MasterName, 
       B.ID BandID, B.Name BandName, 
       C.ID BlackadderID, C.Name BlackadderName
       D.ID BlackadderRealID, D.Name BlackadderRealName
FROM
  TableA A
INNER JOIN
  TableB B
ON
  A.RelatedID = B.ID
INNER JOIN
  TableC C
ON
  B.RelatedID = C.ID
INNER JOIN
  TableD D
ON
  C.RelatedID = D.ID

这将产生以下数据视图:

MasterID  MasterName  BandID  BandName BlackAdderID BlackAdderName  BlackadderRealID  BlackadderRealName
1         John        2       George   1            Bob             1                 Kate
2         Paul        1       Ringo    1            Bob             1                 Kate

It is difficult to tell from your query whether you expect the data to be returned based on a UNION, or as a view containing the columns discretely. This obviously has an effect.

Consider the following sample:

TableA
ID  Name   RelatedID
1   John   2
2   Paul   1

TableB
ID  Name   RelatedID
1   Ringo  1
2   George 1

TableC
ID  Name  RelatedID
1   Bob   1

TableD
ID  Name  RelatedID
1   Kate  NULL

Now, run the following query against this:

SELECT ID, Name FROM TableA
UNION ALL
SELECT ID, Name FROM TableB
UNION ALL
SELECT ID, Name FROM TableC
UNION ALL
SELECT ID, Name FROM TableD

This results in the following output:

1 John
2 Paul
1 Ringo
2 George
1 Bob
1 Kate

Is this what you are after? If so, you use a UNION query.

Now, if the effect you are after is to have a discrete view of related data, you may need to do something like this:

SELECT A.ID MasterID, A.Name MasterName, 
       B.ID BandID, B.Name BandName, 
       C.ID BlackadderID, C.Name BlackadderName
       D.ID BlackadderRealID, D.Name BlackadderRealName
FROM
  TableA A
INNER JOIN
  TableB B
ON
  A.RelatedID = B.ID
INNER JOIN
  TableC C
ON
  B.RelatedID = C.ID
INNER JOIN
  TableD D
ON
  C.RelatedID = D.ID

This will result in the following view of the data:

MasterID  MasterName  BandID  BandName BlackAdderID BlackAdderName  BlackadderRealID  BlackadderRealName
1         John        2       George   1            Bob             1                 Kate
2         Paul        1       Ringo    1            Bob             1                 Kate
吃不饱 2024-07-17 18:21:16

使用 union 语句

select * from table1
union 
select * from table2
union
select * from table3

Use the union statement

select * from table1
union 
select * from table2
union
select * from table3
清风不识月 2024-07-17 18:21:16

如果你联合它们,你可以(我建议包括一些关于每条记录来自哪个表的指示符):

select   table1.column1, 1 as TableNumber
from     table1

union

select   table2.column1, 2 as TableNumber
from     table2

.. etc ..

You can if you union them (I would suggest including some indicator as to which table each record came from):

select   table1.column1, 1 as TableNumber
from     table1

union

select   table2.column1, 2 as TableNumber
from     table2

.. etc ..
不知在何时 2024-07-17 18:21:16

使用 UNION ALL 而不是 UNION,除非您特别希望排除重复行。 UNION 本身需要更长的时间来执行(因为它会进行排序来查找重复项),并且会删除重复的行。

Rather than UNION, use UNION ALL, unless you specifically want duplicate rows to be excluded. UNION on its own takes longer to execute (because of the sort it does to find dupes), and removes duplicate rows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文