CROSS JOIN

原文:CROSS JOIN

最近在讲到T-SQL查询的Join部分时,一下子没有想起来CROSS JOIN的用法,因为其实平常也确实基本不用到。特意找了一个例子,以供参考

CROSS JOIN又称为笛卡尔乘积,实际上是把两个表乘起来。以下资料摘自:http://www.sqlguides.com/sql_cross_join.php


SQL CROSS JOIN will return all records where each row from the first table is combined with each row from the second table. Which also mean CROSS JOIN returns the Cartesian product of the sets of rows from the joined tables.

A CROSS JOIN can be specified in two ways: using the JOIN syntax or by listing the tables in the FROM clause separated by commas without using a WHERE clause to supply join criteria.

SQL CROSS JOIN syntax:

SELECT * FROM [TABLE 1] CROSS JOIN [TABLE 2]

OR

SELECT * FROM [TABLE 1], [TABLE 2]

EXAMPLE :

Let‘s try with 2 tables below:

Table 1: GameScores

PlayerName DepartmentId Scores
Jason 1 3000
Irene 1 1500
Jane 2 1000
David 2 2500
Paul 3 2000
James 3 2000

Table 2: Departments

DepartmentId DepartmentName
1 IT
2 Marketing
3 HR

SQL statement :

SELECT* FROM GameScores CROSS JOIN Departments

Result:

PlayerName DepartmentId Scores DepartmentId DepartmentName
Jason 1 3000 1 IT
Irene 1 1500 1 IT
Jane 2 1000 1 IT
David 2 2500 1 IT
Paul 3 2000 1 IT
James 3 2000 1 IT
Jason 1 3000 2 Marketing
Irene 1 1500 2 Marketing
Jane 2 1000 2 Marketing
David 2 2500 2 Marketing
Paul 3 2000 2 Marketing
James 3 3000 2 Marketing
Jason 1 3000 3 HR
Irene 1 1500 3 HR
Jane 2 1000 3 HR
David 2 2500 3 HR
Paul 3 2000 3 HR
James 3 3000 3 HR
时间: 2024-10-31 16:06:49

CROSS JOIN的相关文章

CROSS JOIN连接用于生成两张表的笛卡尔集

将两张表的情况全部列举出来 结果表: 列= 原表列数相加 行= 原表行数相乘 CROSS JOIN连接用于生成两张表的笛卡尔集. 在sql中cross join的使用: 1.返回的记录数为两个表的记录数乘积. 2.将A表的所有行分别与B表的所有行进行连接. 例如: tableA r1 r2 A B C D tableB r3 r4 1 2 3 4 select * from tableA cross join tableB; return: r1 r2 r3 r4 r1 r2 1 2 r1 r2

SQL中inner join、outer join和cross join的区别

摘自:http://blog.csdn.net/scythe666/article/details/51881235 缺省情况下是inner join,开发中使用的left join和right join属于outer join,另外outer join还包括full join.下面我通过图标让大家认识它们的区别. 现有两张表,Table A 是左边的表.Table B 是右边的表.其各有四条记录,其中有两条记录name是相同的: 1.INNER JOIN 产生的结果是AB的交集 SELECT

SQL SERVER 2012 第四章 连接 JOIN の OUTER JOIN,完全连接FULL JOIN,交叉连接CROSS JOIN

SELECT <SELECT LIST> FROM <the table you want to be the "LEFT" table> <LEFT|RIGHT> [OUTER] JOIN <table you want to be the "RIGHT" table> ON <join condition> 可以看做JOIN之前的表是左表,之后的表是右表. 外部连接本质上是包含的.明确包含的记录取决于使

MySQL的几种连接 join/inner join/cross join/逗号/left join/right join/natural join

转载请注明出处!! 之前数据表连接操作多使用逗号或者join,对几种连接的概念一直浑浑噩噩,最近研究了一波,把这些连接的区别搞明白了. 连接:A xjoin B(主表 操作 关联表) select过程:from->where->group by->having->order by->limit 在不使用on语法时,join.inner join.逗号.cross join结果相同,都是取2个表的笛卡尔积.逗号与其他操作符优先级不同,所以有可能产生语法错误,尽量减少用逗号 jo

SQLSERVER表连接(INNER JOIN,LEFT JOIN,RIGHT JOIN,FULL JOIN,CROSS JOIN,CROSS APPLY,OUTER APPLY)

1 常用表连接(inner join,left join,right join,full join,cross join) if object_id(N'table1',N'U') is not null drop table table1 if object_id(N'table2',N'U') is not null drop table table2 create table table1(id int,name varchar(20)) insert into table1 select

DB2数据库的外连接(OUTER JOIN),内连接(INNER JOIN)和交叉连接(CROSS JOIN)区别

1.交叉连接(CROSS JOIN):有两种,显式的和隐式的,不带ON子句,返回的是两表的乘积,也叫笛卡尔积,返回记录的个数应当是a和b表中符合记录的和. 显式:select [cols_list] from a cross join b where [condition] 隐式:select [cols_list] from a, b where [condition] 2.内连接(INNER JOIN):有两种,显式的和隐式的,返回连接表中符合连接条件和查询条件的数据行,和我们写普通的sql

CROSS JOIN,NATURAL JOIN

CROSS JOIN:笛卡尔积 NATURAL JOIN:

Sql的各种连接用法(cross join、inner join、full join)

1.名词解释: 笛卡尔乘积:笛卡尔乘积是指在数学中,两个集合X和Y的笛卡尔积,又称直积,表示X x Y ,第一个对象是X的成员,而第二个对象       是Y的所有可能有序列的其中的一个成员.假设集合A={a,b},集合B={0,1, 2},则两个笛卡尔积为{(a,0),(a,1),(a,2),         (b,0),(b,1),(b,2)} 2.原址参考:http://ashui.net/archives/2013/552.html 1> 交叉连接CROSS JOIN SELECT *

Linq表连接大全(INNER JOIN、LEFT OUTER JOIN、RIGHT OUTER JOIN、FULL OUTER JOIN、CROSS JOIN)

转载http://www.cnblogs.com/shenqiboy/p/3260105.html 我们知道在SQL中一共有五种JOIN操作:INNER JOIN.LEFT OUTER JOIN.RIGHT OUTER JOIN.FULL OUTER JOIN.CROSS JOIN        内连接.Sql: SELECT [t0].[GroupName], [t1].[UserName] FROM [Group] AS [t0] INNER JOIN [User] AS [t1] ON (

left join ,right join ,inner join ,cross join 区别

left join ,right join ,inner join ,cross join 区别(参考:http://zhidao.baidu.com/link?url=gpOl9HXZR0OrQuys04oH3f8guGQFZBUNs7kd4JWbUcDVbflB4qe1LJnLbfWXa7-CjfDWhzgK5T26-FZpZwVqLq) 举个简单的例子吧,从结果来理解比较具体些.a表name sex张三 男李四 女 b表name age李四 30王五 23 1\全外连接select a.n