`
DavyJones2010
  • 浏览: 154354 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

MySQL: Cartesian Product and Left Join

阅读更多

1. Example as below:

 

# 1. Goods table
select * from goods;
+----------+--------+------------+
| goods_id | cat_id | goods_name |
+----------+--------+------------+
|        1 |      1 | CDMA Phone |
|        2 |      1 | GSM Phone  |
|        3 |      1 | 3G Phone   |
+----------+--------+------------+

# 2. Cat table
select * from cat;
+--------+--------------+
| cat_id | cat_name     |
+--------+--------------+
|      1 | Mobile Phone |
+--------+--------------+

# 3. How to combine cat_name into goods table?

# First approach:
# Using JAVA, select all from goods and get cat_id, then search cat table and get cat_name. Combine results.
# Using m*n times SQL

# Second approach:
# Using join(left join)
# select goods_id, goods_name, cat.cat_name from *** where goods.cat_id = cat.cat_id;
# But how to realize this?

 

 

2. Set/Collection in Math

    1) Properties of Set: Orderless, Uniquity

 

# Orderless
#{1, 2, 3} == {2, 3, 1} == {1, 3, 2}
# Uniquity
# {1, 2, 3, 3} is not a Set

    2) Regard a table as a set, and a row in a table as an element in a set.

 

        So seriously speaking, there can not be two same rows in a single table.

        But actually, there can be two same rows in a single table because there is a rowId for each row in a table. And the rowId is not visible. It is generated by MySQL automatically and can not be accessed by user.

 

3. Cartesian Product in Math

 

# Eg 1

# What's the cartesian product of the two following sets?
# Q: {2, 3, 4} X {4, 7}
# A:  {8, 14, 12, 21, 16, 28}--->Wrong!
# A: {{2, 4}, {2, 7}, {3, 4}, {3, 7}, {4, 4}, {4, 7}}--->Bingo!

#Eg 2

# What's the cartesian product of the two following sets
# Q: {chicken, duck} X {cat, dog, camel}
# A: {{chicken, cat}, {chicken, dog}, {chicken, camel}, {duck, cat}, {duck, dog}, {duck, camel}}

   1) Cartesian product is also called Set multiple.

 

   2) Cartesian product is actually a total combination of the two sets. 

   Q: Let set A have M elements which are unique in set A; Let set B have N elements which are unique in set B. So how many element will we get after cartesian product of A and B?

   A: The number is M*N. And if we put these M*N elements in a set there will not be duplicate elements to worry about

 

.  Q: If table A have 9 columns and table B have 5 columns. What's the cartesian product of the two tables?

   A: There will be 9*5=45 columns.

Eg:

   Cartesian product of two tables (Refer from IBM red brick warehouse website)

 

Join of two tables

State table   Region table City State   City Area Jacksonville Miami Nashville
FL   Jacksonville South
FL Miami South
TN New Orleans South

Example query

select * from state, region;

Cartesian product (join predicate not specified)

City State City Area Jacksonville Jacksonville Jacksonville Miami Miami Miami Nashville Nashville Nashville
FL Jacksonville South
FL Miami South
FL New Orleans South
FL Jacksonville South
FL Miami South
FL New Orleans South
TN Jacksonville South
TN Miami South
TN New Orleans South

 

Eg

#Cartesian product of two tables
select * from goods, cat;
+----------+--------+------------+--------+--------------+
| goods_id | cat_id | goods_name | cat_id | cat_name     |
+----------+--------+------------+--------+--------------+
|        1 |      1 | CDMA Phone |      1 | Mobile Phone |
|        2 |      1 | GSM Phone  |      1 | Mobile Phone |
|        3 |      1 | 3G Phone   |      1 | Mobile Phone |
+----------+--------+------------+--------+--------------+

#A possible solution to the question raised at begining
select * from goods, cat where goods.cat_id = cat.cat_id;
+----------+--------+------------+--------+--------------+
| goods_id | cat_id | goods_name | cat_id | cat_name     |
+----------+--------+------------+--------+--------------+
|        1 |      1 | CDMA Phone |      1 | Mobile Phone |
|        2 |      1 | GSM Phone  |      1 | Mobile Phone |
|        3 |      1 | 3G Phone   |      1 | Mobile Phone |
+----------+--------+------------+--------+--------------+
#Two Steps:
#1. Get cartesian product of two tables
#2. Apply filters to the result set. 

#A more elegant way by using left join
select * from goods left join cat on goods.cat_id = cat.cat_id;
+----------+--------+------------+--------+--------------+
| goods_id | cat_id | goods_name | cat_id | cat_name     |
+----------+--------+------------+--------+--------------+
|        1 |      1 | CDMA Phone |      1 | Mobile Phone |
|        2 |      1 | GSM Phone  |      1 | Mobile Phone |
|        3 |      1 | 3G Phone   |      1 | Mobile Phone |
+----------+--------+------------+--------+--------------+

#Remove duplicate columns
select goods.cat_id, goods_id, goods_name from goods left join cat on goods.cat_id = cat.cat_id;
+--------+----------+------------+
| cat_id | goods_id | goods_name |
+--------+----------+------------+
|      1 |        1 | CDMA Phone |
|      1 |        2 | GSM Phone  |
|      1 |        3 | 3G Phone   |
+--------+----------+------------+

   1) Regard the result of cartesian product or left join as a real table and not as a result set.

   2) We can use where, group by, having, order by, limit on this result table.

       Eg.

select goods.cat_id, goods_id, goods_name from goods left join cat on goods.cat_id = cat.cat_id where goods_id >= 2;
+--------+----------+------------+
| cat_id | goods_id | goods_name |
+--------+----------+------------+
|      1 |        2 | GSM Phone  |
|      1 |        3 | 3G Phone   |
+--------+----------+------------+

 

分享到:
评论

相关推荐

    SQL中的left join right join

    然而,这种方法并不推荐,因为它可能会导致意外的交叉联接(Cartesian product),特别是在有多个表参与联接时。 总结来说,`LEFT JOIN`和`RIGHT JOIN`在处理缺失匹配记录时有所不同,`LEFT JOIN`侧重于左表,`...

    cartesian-product:内存高效的笛卡尔积实现

    安装通过作曲家$ composer require th3n3rd/cartesian-product用法 use Nerd \ CartesianProduct \ CartesianProduct ;$ cartesianProduct = new CartesianProduct ();$ cartesianProduct -> appendSet ( array ( 'a...

    SALE创意海报

    ps文档 日常零售店铺的SALE创意海报分享

    MySQL中的JOIN详解及sql实战

    ##### 示例1:笛卡尔积(Cartesian Product) 笛卡尔积是指在没有指定任何连接条件的情况下,两个表之间的所有可能组合。例如,在MySQL中,如果我们执行以下查询: ```sql SELECT * FROM user_sal s1 JOIN user_sal ...

    前端开源库-cartesian-product

    "前端开源库-cartesian-product"就是这样一个工具,专门用于计算数组的笛卡尔积。笛卡尔积是数学中的一个概念,它是在两个集合的元素之间形成的所有可能配对的集合。在编程中,这个概念常被用来生成所有可能的组合,...

    narx的matlab代码-cartesian_model_free_control:cartesian_model_free_control

    narx的matlab代码笛卡尔无模型控制 该存储库托管用于训练和使用笛卡尔机器人模型的无模型控制器的代码。 该模型类似于(Thuruthel et al。,2018),通过神经网络学习正向动态模型,该训练集是通过运动泡泡过程收集...

    数据库应用技术:SQLJOIN多表查询.pptx

    当两个表进行简单的交叉连接(Cartesian Product)时,如果不加任何限制条件,每一条第一个表的记录都会与第二个表的每一条记录进行组合,产生出大量的无效结果,这被称为笛卡尔积。为了解决这个问题,JOIN操作引入...

    Geodetic2Cartesian.rar_Cartesian_geodetic2cartesian

    converts geodetic to cartesian coords by mozhdeh shahbazi

    go-cartesian-product:用于在golang中构建笛卡尔积的软件包

    go get github.com/schwarmco/go-cartesian-product 用法 import ( "fmt" "github.com/schwarmco/go-cartesian-product" ) func main () { a := [] interface {}{ 1 , 2 , 3 } b := [] interface {}{ "a" , ...

    Cartesian-Genetic-Programming

    ### Cartesian Genetic Programming (CGP):一种创新的自然计算方法 #### 一、概述 《Cartesian Genetic Programming》是一本关于编程领域的经典电子书,它深入探讨了一种名为Cartesian Genetic Programming(简称...

    SQL数据库(Mysql进阶) - Git1

    * 笛卡尔积交叉连接(Cartesian Product):SELECT * FROM 表名 1,表名 2 * 连接中使用别名:SELECT 字段名|表达式 FROM 表名 1 AS 别名 1,表名 2 AS 别名 2 WHERE 查询条件 3. 多表连接 多表连接是指连接三个或...

    php之mysql优化2

    2. **正确使用JOIN**:避免使用过多的JOIN操作,特别是笛卡尔积(Cartesian product)。合理设计数据库关系,减少不必要的关联,或使用子查询替换JOIN。 3. **减少查询复杂性**:将复杂查询拆分为多个简单的查询,...

    combinatorics:将(更多)组合语言引入Ruby

    定义了用于计算功效集,笛卡尔乘积,排列,组合和排列的方法。... 提供可重复使用的Combinatorics Mixins: {Combinatorics :: CartesianProduct} {Combinatorics ::选择} {Combinatorics :: Derange

    25.3 MySQL 多表查询

    8. **笛卡尔积(CARTESIAN JOIN)**:如果没有指定任何连接条件,MySQL 默认执行笛卡尔积,即每个左表记录与右表的每条记录组合。通常这不是我们想要的结果,除非明确知道你在做什么。 在进行多表查询时,应考虑...

    Questions:some questions you may meet, like Cartesian Product(笛卡尔积),Postfix Expression(后缀表达式),and all ……

    Questionssome questions you may meet, like Cartesian Product(笛卡尔积),Postfix Expression(后缀表达式),and all ……

    Python库 | cartesian-0.1.6.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:cartesian-0.1.6.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    经典SQL练习题(MySQL版).docx

    "经典SQL练习题(MySQL版)" 本资源摘要信息来自于一份名为"经典SQL练习题(MySQL版)"的...该资源提供了多种SQL练习题的解决方案,涵盖了HAVING子句、NOT IN子句、Cartesian product、子查询、JOIN操作等多种SQL技术。

    MPC_based-nonlinear-trajectory-planning:该项目与泽徐关于四轮转向车的轨迹规划的硕士论文有关

    标题中的“MPC_based-nonlinear-trajectory-planning”是指一种基于模型预测控制(Model Predictive Control, MPC)的非线性轨迹规划方法,这在四轮转向车辆的控制和路径规划中具有广泛应用。MPC是一种先进的控制...

    cartesian-product-sas:SAS中的笛卡尔积

    在SAS编程环境中,笛卡尔积(Cartesian Product)是一个重要的概念,特别是在处理数据集连接时。笛卡尔积是指从两个或多个数据集中取出每一对元素进行组合,形成一个新的数据集,新数据集中每一条记录都是原数据集中...

Global site tag (gtag.js) - Google Analytics