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
FL | Jacksonville | South | |
FL | Miami | South | |
TN | New Orleans | South |
Example query
select * from state, region;
Cartesian product (join predicate not specified)
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 | +--------+----------+------------+
相关推荐
安装通过作曲家$ composer require th3n3rd/cartesian-product用法 use Nerd \ CartesianProduct \ CartesianProduct ;$ cartesianProduct = new CartesianProduct ();$ cartesianProduct -> appendSet ( array ( 'a...
ps文档 日常零售店铺的SALE创意海报分享
narx的matlab代码笛卡尔无模型控制 该存储库托管用于训练和使用笛卡尔机器人模型的无模型控制器的代码。 该模型类似于(Thuruthel et al。,2018),通过神经网络学习正向动态模型,该训练集是通过运动泡泡过程收集...
前端开源库-cartesian-product笛卡尔积,计算数组的笛卡尔积。
converts geodetic to cartesian coords by mozhdeh shahbazi
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" , ...
WGS84toCartesian-一个简单的仅标头的单文件库,可使用C ++的Mercator投影在WGS84位置(纬度/经度)与笛卡尔位置之间进行转换 WGS84toCartesian是用现代C ++库编写的小型高效库,可使用Mercator投影将WGS84纬度/...
笛卡尔逻辑和命题的可推导性关系,赵峰,,为使集合论和数理逻辑能够适应分析数学的要求,可分析集合被提出,且能表达为P = {x| x∈P}。由此,引入和分析笛卡尔逻辑以使逻辑的�
定义了用于计算功效集,笛卡尔乘积,排列,组合和排列的方法。... 提供可重复使用的Combinatorics Mixins: {Combinatorics :: CartesianProduct} {Combinatorics ::选择} {Combinatorics :: Derange
Questionssome questions you may meet, like Cartesian Product(笛卡尔积),Postfix Expression(后缀表达式),and all ……
资源分类:Python库 所属语言:Python 资源全名:cartesian-0.1.6.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
CARTESIAN TO RECTOPOLAR IN MATLAB
这是关于编程的电子书,高清,最新版本,经典著作,英文版
A.3 The Cartesian Product of Two Vector Spaces 461 A.4 The Tensor Product of Two Vector Spaces 461 A.5 The Kronecker Product AB of Two Matrices 462 B The Jordan Form 466 C Matrix Factorizations 473 ...
笛卡尔积强大的笛卡尔积该模块在v0.0.3之前被称为fast-cartesian-product ,现在分为两个模块: 专注于速度,仅适用于阵列 专注于无限组合,更少的内存,更多的数据类型例子import PowerCartesianProduct from '...
SAS笛卡尔积 1.简介 如果您像我一样,可能很难想象要使用笛卡尔积的情况。 因此,当我发现它对解决的许多分析问题有用时,我感到很惊讶。 在这里,我讨论了在SAS中对笛卡尔乘积进行编程以仅解决以下分析问题之一: ...
cartesian ( storiesOf ( 'Button/Cartesian' , module ) ) . add ( ( ) => ( { colors : [ { bg : '#FF5630' , fg : '#FFBDAD' } , { bg : '#4C9AFF' , fg : '#B3D4FF' } ] , text : [ 'Click Me' , '' , '...
const bigCartesian = require ( 'big-cartesian' ) for ( const values of bigCartesian ( [ [ 'red' , 'blue' ] , [ 'circle' , 'square' ] , ] ) ) { console . log ( values ) } // [ 'red', 'circle' ] // ...
针对CDN网络中海量数据分发的要求,设计了一种基于CDN服务器P2P服务的高效数据分发机制,通过对分割后的海量数据分别执行P2P数据分发,克服了现有CDN服务器P2P服务不能根据Internet网络带宽和CDN服务器负载动态变化...
2 集合(Sets) xÎA x belongs to A / x is an element (or a member) of A xÏA x does not belong to A / x is not an element (or a ...A×B A cross B / the Cartesian product of A and B(A与B的 笛卡尔积)