合并两个没有公共字段的表
我想学习如何组合两个没有共同字段的数据库表。 我查过 UNION 但 MSDN 说:
以下是使用 UNION 组合两个查询的结果集的基本规则:
- 所有查询中的列数和顺序必须相同。
- 数据类型必须兼容。
但我完全没有共同领域。 我想要的只是将它们像视图一样组合在一张表中。
所以我该怎么做?
I want to learn how to combine two db tables which have no fields in common. I've checked UNION but MSDN says :
The following are basic rules for combining the result sets of two queries by using UNION:
- The number and the order of the columns must be the same in all queries.
- The data types must be compatible.
But I have no fields in common at all. All I want is to combine them in one table like a view.
So what should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
有多种方法可以做到这一点,具体取决于您的真正需求。 由于没有通用色谱柱,您需要决定是要引入通用色谱柱还是购买该产品。
假设您有两个表:
忘记实际的列,因为在这种情况下您很可能拥有客户/订单/零件关系; 我只是用这些专栏来说明如何做到这一点。
笛卡尔积会将第一个表中的每一行与第二个表中的每一行进行匹配:
这可能不是您想要的,因为 1000 个零件和 100 个客户将导致 100,000 行包含大量重复信息。
或者,您可以使用联合来仅输出数据,尽管不是并排(您需要确保两个选择之间的列类型兼容,可以通过使表列兼容或在选择中强制它们) ):
在某些数据库中,您可以使用 rowid/rownum 列或伪列来并排匹配记录,例如:
代码将类似于:
它仍然像笛卡尔积但是
where
子句限制了如何组合行以形成结果(因此根本不是笛卡尔积,真的)。我还没有为此测试该 SQL,因为它是我选择的 DBMS 的限制之一,因此,我不认为在经过深思熟虑的模式中需要它。 由于 SQL 不保证其生成数据的顺序,因此每次执行查询时匹配都可能会发生变化,除非您有特定关系或
order by
子句。我认为理想的做法是向两个表添加一列,指定关系是什么。 如果没有真正的关系,那么您可能没有必要尝试将它们与 SQL 并列放置。
如果您只想将它们并排显示在报告或网页上(两个示例),那么正确的工具就是生成您的报告或网页的任何工具,再加上两个独立 SQL查询来获取两个不相关的表。 例如,BIRT(或 Crystal 或 Jasper)中的两列网格每个都有一个单独的数据表,或者 HTML 两列表(或 CSS)每个都有一个单独的数据表。
There are a number of ways to do this, depending on what you really want. With no common columns, you need to decide whether you want to introduce a common column or get the product.
Let's say you have the two tables:
Forget the actual columns since you'd most likely have a customer/order/part relationship in this case; I've just used those columns to illustrate the ways to do it.
A cartesian product will match every row in the first table with every row in the second:
That's probably not what you want since 1000 parts and 100 customers would result in 100,000 rows with lots of duplicated information.
Alternatively, you can use a union to just output the data, though not side-by-side (you'll need to make sure column types are compatible between the two selects, either by making the table columns compatible or coercing them in the select):
In some databases, you can use a rowid/rownum column or pseudo-column to match records side-by-side, such as:
The code would be something like:
It's still like a cartesian product but the
where
clause limits how the rows are combined to form the results (so not a cartesian product at all, really).I haven't tested that SQL for this since it's one of the limitations of my DBMS of choice, and rightly so, I don't believe it's ever needed in a properly thought-out schema. Since SQL doesn't guarantee the order in which it produces data, the matching can change every time you do the query unless you have a specific relationship or
order by
clause.I think the ideal thing to do would be to add a column to both tables specifying what the relationship is. If there's no real relationship, then you probably have no business in trying to put them side-by-side with SQL.
If you just want them displayed side-by-side in a report or on a web page (two examples), the right tool to do that is whatever generates your report or web page, coupled with two independent SQL queries to get the two unrelated tables. For example, a two-column grid in BIRT (or Crystal or Jasper) each with a separate data table, or a HTML two column table (or CSS) each with a separate data table.
这是一个非常奇怪的请求,几乎可以肯定这是您在现实应用程序中永远不想做的事情,但从纯粹的学术角度来看,这是一个有趣的挑战。 在 SQL Server 2005 中,您可以使用通用表表达式和 row_number() 函数并进行连接:
这可行,但非常愚蠢,我仅将其作为“社区 wiki”答案提供,因为我真的不会推荐它。
This is a very strange request, and almost certainly something you'd never want to do in a real-world application, but from a purely academic standpoint it's an interesting challenge. With SQL Server 2005 you could use common table expressions and the row_number() functions and join on that:
This works, but it's supremely silly and I offer it only as a "community wiki" answer because I really wouldn't recommend it.
这会将 table1 中的每一行与 table2 (笛卡尔积)连接起来,返回所有列。
This will join every row in table1 with table2 (the Cartesian product) returning all columns.
尝试:
这将从两个表中获取所有列。
try:
This will bring all the columns from both the table.
如果表没有公共字段,则无法在任何有意义的视图中组合数据。 您最终更有可能得到一个包含两个表中重复数据的视图。
If the tables have no common fields then there is no way to combine the data in any meaningful view. You would more likely end up with a view that contains duplicated data from both tables.
为了获得两个表的有意义/有用的视图,您通常需要从每个表中确定一个标识字段,然后可以在 JOIN 的 ON 子句中使用该字段。
那么在您看来:
您提到没有字段是“通用的”,但是尽管标识字段可能不具有相同的名称,甚至不是相同的数据类型,但您可以使用转换/转换函数以某种方式连接它们。
To get a meaningful/useful view of the two tables, you normally need to determine an identifying field from each table that can then be used in the ON clause in a JOIN.
THen in your view:
You mention no fields are "common", but although the identifying fields may not have the same name or even be the same data type, you could use the convert / cast functions to join them in some way.
为什么不使用简单的方法
它为您提供两个表中的所有列,并返回来自客户和供应商的所有记录,如果客户有 3 条记录,供应商有 2 条记录,那么供应商将在所有列中显示 NULL
why don't you use simple approach
It gives you all columns from both tables and returns all records from customer and supplier if Customer has 3 records and supplier has 2 then supplier'll show NULL in all columns
如果它拥抱数据,则需要很长时间..
if its hug data , its take long time ..
连接不相关的表
演示 SQL 脚本
要连接非相关表,我们将介绍一个序列号的常见连接列如下。
SQL 脚本
Joining Non-Related Tables
Demo SQL Script
To Join Non-Related Tables , we are going to introduce one common joining column of Serial Numbers like below.
SQL Script
当您必须使用三个选择语句来执行此操作时非常困难,
我尝试了所有建议的技术,但这是徒劳的
请参阅下面的脚本。 如果您有替代解决方案,请提出建议
Very hard when you have to do this with three select statments
I tried all proposed techniques up there but it's in-vain
Please see below script. please advice if you have alternative solution
如果表有唯一的字段标识符,我们只需要匹配它们,忘记逻辑关系,我们可以使用这段代码
If the tables have unique field identifiers and we just need to match them, forgetting about the logical relationship, we can use this code
请尝试以下查询:
合并两个没有公共列的表:
Please try this query:
Combine two tables that have no common columns: