以MySql为例。在MySQL数据库中建立两张数据表,并分别插入一些数据。 示例脚本如下: drop table table1; CREATE TABLE `andrew`.`table1` ( ` name ` VARCHAR (32) NOT NULL , `city` VARCHAR (32) NOT NULL ) ENGINE=MyISAM; insert into TABLE1( name ,ci
以MySql为例。在MySQL数据库中建立两张数据表,并分别插入一些数据。
示例脚本如下:
<ol class="dp-sql"> <li class="alt"><span><span> </span><span class="keyword">drop</span><span> </span><span class="keyword">table</span><span> table1; </span></span></li> <li> <span></span><span class="keyword">CREATE</span><span> </span><span class="keyword">TABLE</span><span> `andrew`.`table1` </span> </li> <li class="alt"><span>( </span></li> <li> <span>`</span><span class="keyword">name</span><span>` </span><span class="keyword">VARCHAR</span><span>(32) </span><span class="op">NOT</span><span> </span><span class="op">NULL</span><span>, </span> </li> <li class="alt"> <span>`city` </span><span class="keyword">VARCHAR</span><span>(32) </span><span class="op">NOT</span><span> </span><span class="op">NULL</span><span> </span> </li> <li><span>) </span></li> <li class="alt"><span>ENGINE = MyISAM; </span></li> <li> <span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE1(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person A'</span><span>, </span><span class="string">'BJ'</span><span>); </span> </li> <li class="alt"> <span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE1(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person B'</span><span>, </span><span class="string">'BJ'</span><span>); </span> </li> <li> <span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE1(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person C'</span><span>, </span><span class="string">'SH'</span><span>); </span> </li> <li class="alt"> <span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE1(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person D'</span><span>, </span><span class="string">'SZ'</span><span>); </span> </li> <li> <span></span><span class="keyword">commit</span><span>; </span> </li> <li class="alt"> <span></span><span class="keyword">drop</span><span> </span><span class="keyword">table</span><span> table2; </span> </li> <li> <span></span><span class="keyword">CREATE</span><span> </span><span class="keyword">TABLE</span><span> `andrew`.`table2` </span> </li> <li class="alt"><span>( </span></li> <li> <span>`</span><span class="keyword">name</span><span>` </span><span class="keyword">VARCHAR</span><span>(32) </span><span class="op">NOT</span><span> </span><span class="op">NULL</span><span>, </span> </li> <li class="alt"> <span>`city` </span><span class="keyword">VARCHAR</span><span>(32) </span><span class="op">NOT</span><span> </span><span class="op">NULL</span><span> </span> </li> <li><span>) </span></li> <li class="alt"><span>ENGINE = MyISAM; </span></li> <li> <span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE2(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person W'</span><span>, </span><span class="string">'BJ'</span><span>); </span> </li> <li class="alt"> <span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE2(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person X'</span><span>, </span><span class="string">'SH'</span><span>); </span> </li> <li> <span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE2(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person Y'</span><span>, </span><span class="string">'SH'</span><span>); </span> </li> <li class="alt"> <span></span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> TABLE2(</span><span class="keyword">name</span><span>, city) </span><span class="keyword">values</span><span> (</span><span class="string">'Person Z'</span><span>, </span><span class="string">'NJ'</span><span>); </span> </li> <li> <span></span><span class="keyword">commit</span><span>; </span> </li> </ol>
1. MySQL外连接 – 左连接结果
table1居左,故谓之左连接。这种情况下,以table1为主,即table1中的所有记录均会被列出。有一下三种情况:
a. 对于table1中的每一条记录对应的城市如果在table2中也恰好存在而且刚好只有一条,那么就会在
返回的结果中形成一条新的记录。如上面Person A和Person B对应的情况。
b. 对于table1中的每一条记录对应的城市如果在table2中也恰好存在而且有N条,那么就会在返回的结果中形成N条新的记录。如上面的Person C对应的情况。
c. 对于table1中的每一条记录对应的城市如果在table2中不存在,那么就会在返回的结果中形成一条
条新的记录,且该记录的右边全部NULL。如上面的Person D对应的情况。
不符合上面三条规则的记录不会被列出。
2. MySQL外连接 – 右连接结果
table2居右,故谓之右连接。这种情况下,以table2为主,即table2中的所有记录均会被列出。有一下三种情况:
a. 对于table2中的每一条记录对应的城市如果在table1中也恰好存在而且刚好只有一条,那么就会在
返回的结果中形成一条新的记录。如上面Person X和Person Y对应的情况。
b. 对于table2中的每一条记录对应的城市如果在table1中也恰好存在而且有N条,那么就会在返回的结果中形成N条新的记录。如上面的Person W对应的情况。
c. 对于table2中的每一条记录对应的城市如果在table1中不存在,那么就会在返回的结果中形成一条
条新的记录,且该记录的左边全部NULL。如上面的Person Z对应的情况。
不符合上面三条规则的记录不会被列出。
3. MySQL内连接
MySQL内连接的数据记录中,不会存在字段为NULL的情况。可以简单地认为,内链接的结果就是在左连接或者右连接的结果中剔除存在字段为NULL的记录后所得到的结果。甚至可以认为,如果两个表中仅分别剩下内连接运算后所得的数据记录,如table1中只有Person A、Person B和Person C,table2中只有Person W、Person X和Person Y,那么这两个表的之间的左连接和右连接的返回的结果是一样的。
注意:select * from table1 a inner join table2 b on a.city = b.city 和select * from table1 a join table2 b on a.city = b.city 的效果是一样的,即如果join的左边没有诸如left、right或者inner这样的关键字时,缺省的是内连接。另外,MySQL不支持full join。