【发布时间】:2015-09-06 10:50:32
【问题描述】:
我无法理解笛卡尔积的工作原理。考虑简单的模式:
mysql> select * from account;
+----------------+-------------+---------+
| account_number | branch_name | balance |
+----------------+-------------+---------+
| A101 | Downtown | 500 |
| A102 | Perryridge | 400 |
| A201 | Brighton | 900 |
| A215 | Mianus | 700 |
| A217 | Brighton | 750 |
| A222 | Redwood | 700 |
| A305 | Round Hill | 350 |
+----------------+-------------+---------+
7 rows in set (0.00 sec)
现在,当我提出查询时
select a.balance from account a, account b where a.balance<b.balance;
我得到一系列值,除了最大值 900。然后使用not in 运算符确定最大值。在上述查询之前,当基于条件a.balance<b.balance 进行连接时,关系中的第一个元组必须是500。理论上,前 5 个值必须是:
500
500
500
500
400
但我明白了:
+---------+
| balance |
+---------+
| 400 |
| 350 |
| 350 |
| 500 |
| 400 |
它是如何工作的?我正在使用 MySQL 数据库。
【问题讨论】:
-
一个笛卡尔积的命名会让人误以为它是线性的,而不是指数的,实际上它是。也就是说,这是一个很棒的流行词,可以吓跑初级程序员
标签: mysql sql select join cartesian-product