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
CityState CityArea
JacksonvilleFL JacksonvilleSouth
MiamiFLMiamiSouth
NashvilleTNNew OrleansSouth

Example query

select * from state, region;

Cartesian product (join predicate not specified)

CityStateCityArea
JacksonvilleFLJacksonvilleSouth
JacksonvilleFLMiamiSouth
JacksonvilleFLNew OrleansSouth
MiamiFLJacksonvilleSouth
MiamiFLMiamiSouth
MiamiFLNew OrleansSouth
NashvilleTNJacksonvilleSouth
NashvilleTNMiamiSouth
NashvilleTNNew OrleansSouth

 

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   |
+--------+----------+------------+

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值