left join, right join , inner join, join, union的意义
left join,right join,inner join, join 与 on
配合用
select
col_name.. from tablename1 [inner | left | right] join tablename2 on
on_condition
select col_name.. from
tablename1 left join tablename2 on on_condition
CREATE TABLE IF NOT EXISTS `name`(
pid int(11) unsigned auto_increment,
name varchar(40) NOT NULL,
primary key(`pid`)
) ENGINE = MYISAM DEFAULT CHARSET = UTF8;
CREATE TABLE IF NOT EXISTS `sname`(
id int(11) unsigned NOT NULL AUTO_INCREMENT,
spid int(11) NOT NULL,
firstname varchar(40),
primary key(`id`)
)ENGINE = MYISAM DEFAULT CHARSET=UTF8;
name与sname 中,name.pid 与 sname.spid 相关联
name 表数据:

sname 表数据

执行时当tablename1[(如果在后面单独附加有条件时)符合条件时]全部选择,在tablename2时如果没有相应的数据,则其列都显示为NULL
right
join类似,只是角色反过来了
inner join (与 join 相同)
表示两个表都有的数据行
相当于 from tabname1,tabname2 where ? = ?
具体实例如下:
其实就是两表的笛卡尔积
用一个表的一行去乘另一个表的所有行,然后再换到一下行,重复相乘另一个表的所有行,直接这个表结尾
$sql = "select * from name, sname";
$result = mysql_query($sql, $link);
$i = 0;
echo '<table border="1" width="550">';
echo '<tr>';
echo '<th width="40">行号</th>';
echo '<th width="80">name表pid</th>';
echo '<th width="80">name表name</th>';
echo '<th width="80">sname表id</th>';
echo '<th width="100">sname表spid</th>';
echo '<th width="140">sname表firstname</th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
$i++;
echo '<tr>';
echo '<td>'.$i.'</td>';
echo '<td>'.$row['pid'].'</td>';
echo '<td>'.$row['name'].'</td>';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['spid'].'</td>';
echo '<td>'.$row['firstname'].'</td>';
echo '</tr>';
}
echo '</table>';
结果为:

其实就是选择两边都有的字段,且满足 where 条件
$sql = "select * from name, sname where name.pid = sname.spid";
$result = mysql_query($sql, $link);
$i = 0;
echo '<table border="1" width="550">';
echo '<tr>';
echo '<th width="40">行号</th>';
echo '<th width="80">name表pid</th>';
echo '<th width="80">name表name</th>';
echo '<th width="80">sname表id</th>';
echo '<th width="100">sname表spid</th>';
echo '<th width="140">sname表firstname</th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
$i++;
echo '<tr>';
echo '<td>'.$i.'</td>';
echo '<td>'.$row['pid'].'</td>';
echo '<td>'.$row['name'].'</td>';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['spid'].'</td>';
echo '<td>'.$row['firstname'].'</td>';
echo '</tr>';
}
echo '</table>';

如果用 join,inner join 结果是一样的,即
$sql = "select * from name (inner) join sname on name.pid = sname.spid"
以下是用 left join
其实就是选择左边所有的数据,如果右边没有与左边相关的就为NULL
$sql = "select * from name left join sname on name.pid = sname.spid";
$result = mysql_query($sql, $link);
$i = 0;
echo '<table border="1" width="550">';
echo '<tr>';
echo '<th width="40">行号</th>';
echo '<th width="80">name表pid</th>';
echo '<th width="80">name表name</th>';
echo '<th width="80">sname表id</th>';
echo '<th width="100">sname表spid</th>';
echo '<th width="140">sname表firstname</th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
$i++;
echo '<tr>';
echo '<td>'.$i.'</td>';
echo '<td>'.$row['pid'].'</td>';
echo '<td>'.$row['name'].'</td>';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['spid'].'</td>';
echo '<td>'.$row['firstname'].'</td>';
echo '</tr>';
}
echo '</table>';

right join 正好相反
其实就选择右表的所有数据,如果左边没有相关的就为NULL
$sql = "select * from name right join sname on name.pid = sname.spid";
$result = mysql_query($sql, $link);
$i = 0;
echo '<table border="1" width="550">';
echo '<tr>';
echo '<th width="40">行号</th>';
echo '<th width="80">name表pid</th>';
echo '<th width="80">name表name</th>';
echo '<th width="80">sname表id</th>';
echo '<th width="100">sname表spid</th>';
echo '<th width="140">sname表firstname</th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
$i++;
echo '<tr>';
echo '<td>'.$i.'</td>';
echo '<td>'.$row['pid'].'</td>';
echo '<td>'.$row['name'].'</td>';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['spid'].'</td>';
echo '<td>'.$row['firstname'].'</td>';
echo '</tr>';
}
echo '</table>';

union把选择的数据组合返回
AS:
select colname1...
union [ALL |
DISTINCT]
select colname2....
union 一般要求选择的字段数目相同,对应的字段类型也得相同.
在mysql
5.1版本中
如果union连接的字段数不相等,则报错,如果字段数相同,字段类型不同,以返回的结果全部填充在第一个 select 的字段中
left join, right join , inner join, join, union的意义的更多相关文章
- 【Transact-SQL】SQL Server自动把left join自动转化为inner join、以及关联时的数据重复问题
原文:[Transact-SQL]SQL Server自动把left join自动转化为inner join.以及关联时的数据重复问题 1.SQL Server自动把left join自动转化为inn ...
- 数据库中的左连接(left join)和右连接(right join)区别
Left Join / Right Join /inner join相关 关于左连接和右连接总结性的一句话: 左连接where只影向右表,右连接where只影响左表. Left Join select ...
- MySQL的JOIN(四):JOIN优化实践之快速匹配
这篇博文讲述如何优化扫描速度.我们通过MySQL的JOIN(二):JOIN原理得知了两张表的JOIN操作就是不断从驱动表中取出记录,然后查找出被驱动表中与之匹配的记录并连接.这个过程的实质就是查询操作 ...
- MySQL的JOIN(五):JOIN优化实践之排序
这篇博文讲述如何优化JOIN查询带有排序的情况.大致分为对连接属性排序和对非连接属性排序两种情况.插入测试数据. CREATE TABLE t1 ( id INT PRIMARY KEY AUTO_I ...
- Python3 join函数和os.path.join用法
Python3 join函数和os.path.join用法 os.path.join()连接两个文件名地址的时候,就比os.path.join("D:\","test. ...
- Python join() 方法与os.path.join()的区别
Python join() 方法与os.path.join()的区别 pythonJoinos.path.join 今天工作中用到python的join方法,有点分不太清楚join() 方法与os.p ...
- “,”、“natural join”、“natural left outer join”、“natural right outer join”的用法总结
“,”:代表笛卡尔积: “natural join”:代表自然连接,即同名列等值连接: “natural left outer join”:表示左外连接: “natural right outer j ...
- hive 包含操作(left semi join)(left outer join = in)迪卡尔积
目前hive不支持 in或not in 中包含查询子句的语法,所以只能通过left join实现. 假设有一个登陆表login(当天登陆记录,只有一个uid),和一个用户注册表regusers(当天注 ...
- left join on and和left join on where条件的困惑[转]
外连接:left join(左联接) left outer join 返回包括左表中的所有记录和右表中联结字段相等的记录right join(右联接) right outer join返回包括右表中的 ...
- Oracle数据库,join多表关联方式、union结果集合并
join on : 多表关联 内连接 :与其他表连接 from 表1 t join 表2 s on t.字段1 =s.字段2 join 表3 n on n.字段3=t.字段1 或 from 表1 ...
随机推荐
- 高性能以太网芯片W5500 数据手册 V1.0(二)
继续给大家介绍W5500 数据手册. 2.4 固定数据长度模式(FDM) 在外设主机不能控制 SCSn 时,可以使用固定数据长度模式. 此时,SCSn 必须连接到低电平(保持接地).与此同 ...
- linux下利用openssl来实现证书的颁发(详细步骤)--转载和修改
原文地址:http://www.cnblogs.com/firtree/p/4028354.html linux下利用openssl来实现证书的颁发(详细步骤) 1.首先需要安装openssl,一个开 ...
- 【javascript】复制到剪贴板功能(支持目前各种浏览器)
本demo支持各种浏览器复制,亲测可用(IE8,IE9,IE10,火狐,谷歌). 本demo中使用了ZeroClipboard(下载地址:https://github.com/zeroclipboar ...
- OcciWrapper使用指南(高性能Oracle访问组件)
occiwrapper使用指南 occiwrapper是一个开源的.跨平台的Oracle访问组件, 方便C++开发者们灵活地操作oracle数据库.为了方便使用,组件中的接口形式参考的POCO库的使用 ...
- C#下的 Emgu CV
Emgu CV下载地址 http://sourceforge.net/projects/emgucv/files/ 找最新的下就行了,傻瓜式安装,选择目录后自动完成安装,然后提示安装VS2008和VS ...
- asp.net中为什么修改了配置文件后我们不需要重启IIS
本文转载:http://blog.itpub.net/12639172/viewspace-659819/ 大家知道,asp.net中,如果我们修改了配置文件只要把它保存之后,就会立刻反应到程序中, ...
- 在Linux环境中使用Ext3文件系统
Linux缺省情况下使用的文件系统为Ext2,ext2文件系统的确高效稳定.但是,随着Linux系统在关键业务中的应用,Linux文件系统的弱点也渐渐显露出来了:其中系统缺省使用的ext2文件系统 ...
- CCLabelAtlas创建自定义字体
有时候游戏中要用到一些特殊的字体效果,特别是数字. CCLabelAtlas就可以从png图中读取文字. CCLabelAtlas* diceCount=CCLabelAtlas::labelWith ...
- 加速Android Studio/Gradle构建
已经使用Android Studio进行开发超过一年,随着项目的增大,依赖库的增多,构建速度越来越慢,现在最慢要6分钟才能build一个release的安装包,在网上查找资料,发现可以通过一些配置可以 ...
- 使用HttpURLConnection实现在android客户端和服务器之间传递对象
一般情况下,客户端和服务端的数据交互都是使用json和XML,相比于XML,json更加轻量级,并且省流量,但是,无论我们用json还是用xml,都需要我们先将数据封装成json字符串或者是一个xml ...