最近在讲到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

SQL CROSS JOIN相关推荐

  1. sql join 示例_SQL CROSS JOIN与示例

    sql join 示例 In this article, we will learn the SQL CROSS JOIN concept and support our learnings with ...

  2. SQL查询cross join 的用法(笛卡尔积)

    [转自]http://www.cnblogs.com/chenxizhang/archive/2008/11/10/1330325.html CROSS JOIN又称为笛卡尔乘积,实际上是把两个表乘起 ...

  3. sql中join类型_SQL Join类型概述和教程

    sql中join类型 This article will provide an overview of the SQL Join and cover all of the SQL join types ...

  4. Spark SQL中出现 CROSS JOIN 问题解决

    Spark SQL中出现 CROSS JOIN 问题解决 参考文章: (1)Spark SQL中出现 CROSS JOIN 问题解决 (2)https://www.cnblogs.com/yjd_hy ...

  5. SQL中cross join,left join,right join ,full join,inner join 的区别

    http://blog.csdn.net/sgivee/article/details/5081350 SQL中cross join,left join,right join ,full join,i ...

  6. SQL的各种连接(cross join、inner join、full join)的用法理解

    SQL中的连接可以分为内连接,外连接,以及交叉连接 . 1. 交叉连接CROSS JOIN 如果不带WHERE条件子句,它将会返回被连接的两个表的笛卡尔积,返回结果的行数等于两个表行数的乘积: 举例, ...

  7. 关于SQL数据库中cross join 和inner join用法上的区别?

    使用mysql创建两张表 表a 表b 可以使用下面的脚本创建表,并且添加测试数据: CREATE TABLE `a`  (   `name` varchar(10) CHARACTER SET utf ...

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

    对于SQL中inner join.outer join和cross join的区别很多人不知道,我也是别人问起,才查找资料看了下,跟自己之前的认识差不多,如果你使用join连表,缺陷的情况下是inne ...

  9. SQL——左连接(Left join)右连接(Right join)内连接(Inner join) 笛卡尔积(Cross Join)

    ​Left join 即左连接,是以左表为基础,根据ON后给出的两表的条件将两表连接起来.结果会将左表所有的查询信息列出,而右表只列出ON后条件与左表满足的部分.左连接全称为左外连接,是外连接的一种. ...

最新文章

  1. 【实用】一个移动端简单的UI弹窗组件,虽算不上高大上,但至少耐看
  2. UIBezierPath路径绘图
  3. Linux局域网搭建
  4. 早停 tf.keras.callbacks.EarlyStopping() 详解【TensorFlow2入门手册】
  5. 开源]OSharpNS 步步为营系列 - 1. 业务模块设计
  6. 2019ACM浪潮杯山东省赛参赛总结
  7. 论文浅尝 - ICLR2020 | Pretrained Encyclopedia: 弱监督知识预训练语言模型
  8. 用python实现yale人脸数据集降维_用Python实现磁盘IO操作全攻略,让数据流动起来!...
  9. OpenShift 4 - 解决 OpenShift 中 elasticsearch 环境的 Log4j 漏洞
  10. 计算机网络class 3(速率的相关性能指标)
  11. express url跳转(重定向)的实现:res.location() res.redirect()
  12. 非 ROOT 安卓内录
  13. 只需8招,搞定Pandas数据筛选与查询
  14. QTtabbar只有几个选项, 没有“Qttabar”这个选项
  15. SpringBoot+logback优雅的配置日志!
  16. pandas 透视表bug InvalidIndexError: Reindexing only valid with uniquely valued Index objects
  17. Linux常见英文报错中文翻译
  18. SQL Server Always Encrypted
  19. 基于MFC的网络浏览器Demo
  20. ASp.net判断文件或文件夹是否存在

热门文章

  1. [测试]将TestCase整合
  2. 《CCNA安全640-554认证考试指南》——6.4节复习所有考试要点
  3. Android之SharedPreferences两个工具类
  4. windows server 2008 让domain user有本地administrator权限
  5. mysql大小写问题
  6. 田志刚:人跟人差距怎么那么大呢?
  7. Office 2007 文件扩展名类型
  8. Kernel Video Converter中文版
  9. Light Image Resizer6中文版
  10. 六、jQuery基础