SQL 从表1、表2中选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39859022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:54:49  来源:igfitidea点击:

Select from Table1, Table2

sqlselect

提问by Asiri Dissanayaka

I found the following query and appreciate it if someone can help explain to me what this means.

我找到了以下查询,如果有人能帮助我解释这意味着什么,我将不胜感激。

select * from table1, table2

回答by Kamil Gosciminski

This is called CROSS JOINbut using old syntax with ,in FROMclause. My advice is not to use old syntax, stick with the JOINhere.

这被称为CROSS JOIN但使用带有,inFROM子句的旧语法。我的建议是不要使用旧语法,坚持JOIN这里。

It produces a cartesian product, so the number of rows in the result set will be the number of rows from table1multiplied by number of rows from table2(assuming there are no constraints in the WHEREclause). It effectively pairs each row from table1with a row coming from table2.

它产生笛卡尔积,因此结果集中的行数将是 from 的行table1数乘以 from 的行数table2(假设WHERE子句中没有约束)。它有效地将来自 的每一行与来自table1的行配对table2

Below query is an equivalent but does explicit JOINoperation which separates constraint logic of data retrieval (normally put within the WHEREclause) from logic of connecting related data stored across separate tables (within the FROMclause):

下面的查询是等价的,但执行显式JOIN操作,它将数据检索的约束逻辑(通常放在WHERE子句中)与连接存储在不同表中的相关数据的逻辑(FROM子句内)分开:

SELECT *
FROM table1
CROSS JOIN table2

Consider an example where table1has 8 rows and table2has 5 rows. In the output, you get 40 rows (8 rows * 5 rows), because it pairs all rows from both sources (tables).

考虑一个例子,其中table1有 8 行和table25 行。在输出中,您得到 40 行(8 行 * 5 行),因为它将来自两个源(表)的所有行配对。

回答by Chim Chimz

You will get all rows from table1 multiplied by all rows from table2, and will display depending on the columns of both tables. As @sgeddes pointed out, creating a cartesian product.

您将获得 table1 中的所有行乘以 table2 中的所有行,并将根据两个表的列进行显示。正如@sgeddes 所指出的,创建笛卡尔积。

回答by Amol B Lande

Table1 (Col1, Col2) with 4 Records

表 1 (Col1, Col2) 有 4 条记录

Table2 (Col11, Col22, Col33) with 3 Records

表 2(Col11、Col22、Col33)有 3 条记录

when you use the query given below, It will produce NxM number of rows (Cartesian Join)

当您使用下面给出的查询时,它将产生 NxM 行数(笛卡尔连接)

select * from table1, table2 

The result and column sequence from both tables would be given below with 4 x 3 = 12 Records. Col1,Col2, Col11, Col22, Col33

下面将给出两个表的结果和列序列,其中包含 4 x 3 = 12 条记录。第 1 列、第 2 列、第 11 列、第 22 列、第 33 列