数据库在连接两张或以上的表来返回数据时,都会生成一张中间的临时表,然后再将临时表返回给用户
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的意义的更多相关文章

  1. 【Transact-SQL】SQL Server自动把left join自动转化为inner join、以及关联时的数据重复问题

    原文:[Transact-SQL]SQL Server自动把left join自动转化为inner join.以及关联时的数据重复问题 1.SQL Server自动把left join自动转化为inn ...

  2. 数据库中的左连接(left join)和右连接(right join)区别

    Left Join / Right Join /inner join相关 关于左连接和右连接总结性的一句话: 左连接where只影向右表,右连接where只影响左表. Left Join select ...

  3. MySQL的JOIN(四):JOIN优化实践之快速匹配

    这篇博文讲述如何优化扫描速度.我们通过MySQL的JOIN(二):JOIN原理得知了两张表的JOIN操作就是不断从驱动表中取出记录,然后查找出被驱动表中与之匹配的记录并连接.这个过程的实质就是查询操作 ...

  4. MySQL的JOIN(五):JOIN优化实践之排序

    这篇博文讲述如何优化JOIN查询带有排序的情况.大致分为对连接属性排序和对非连接属性排序两种情况.插入测试数据. CREATE TABLE t1 ( id INT PRIMARY KEY AUTO_I ...

  5. Python3 join函数和os.path.join用法

    Python3  join函数和os.path.join用法 os.path.join()连接两个文件名地址的时候,就比os.path.join("D:\","test. ...

  6. Python join() 方法与os.path.join()的区别

    Python join() 方法与os.path.join()的区别 pythonJoinos.path.join 今天工作中用到python的join方法,有点分不太清楚join() 方法与os.p ...

  7. “,”、“natural join”、“natural left outer join”、“natural right outer join”的用法总结

    “,”:代表笛卡尔积: “natural join”:代表自然连接,即同名列等值连接: “natural left outer join”:表示左外连接: “natural right outer j ...

  8. hive 包含操作(left semi join)(left outer join = in)迪卡尔积

    目前hive不支持 in或not in 中包含查询子句的语法,所以只能通过left join实现. 假设有一个登陆表login(当天登陆记录,只有一个uid),和一个用户注册表regusers(当天注 ...

  9. left join on and和left join on where条件的困惑[转]

    外连接:left join(左联接) left outer join 返回包括左表中的所有记录和右表中联结字段相等的记录right join(右联接) right outer join返回包括右表中的 ...

  10. 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 ...

随机推荐

  1. PAT 1057. Stack (30)

    题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1057 用树状数组和二分搜索解决,对于这种对时间复杂度要求高的题目,用C的输入输出显然更好 #i ...

  2. Spring BOOT PERFORMANCE

    转自:http://www.alexecollins.com/spring-boot-performance/ 官方优化文档: https://spring.io/blog/2015/12/10/sp ...

  3. JBPM学习(四):执行流程实例

    概念: ProcessInstance,流程实例:代表流程定义的一次执行.如:张三昨天按请假流程请了一次假.一个流程实例包括了所有运行阶段,其中最典型的属性就是跟踪当前节点的指针,如下图. Execu ...

  4. Java-日历表

    效果图 import java.util.*; import java.text.*; class demo { public static void main(String[] args) { // ...

  5. iframe框架自适应高度 uncanght SecurityError: Blocked a frame with origin "null" from accessing a frame ....

    来源于crm项目的contact/edit.html 一.背景是这样的 最近在做crm系统的前端页面,有一个页面呢,点击“查看全部信息”时会弹出,这个弹窗里面又有分页导航,分页不是使用ajax 异步刷 ...

  6. iOS开发中WebP格式的64位支持处理

    几个月前我们项目中添加了对webp格式的处理.期间遇到了一些问题,这是当中的一个小的记录. 官方下载地址:https://code.google.com/p/webp/downloads/list 对 ...

  7. CenOS搭建FTP服务器

    CenOS搭建FTP服务器 -------------------------------------------------------------------------准备工作--------- ...

  8. winform button设计(一)

    对于winform的button设计来说,vs真心没有给太多的样式布局.为了能给予用户更加人性化的界面.我们在做程序时往往会设计美观的button. 比方,我今天在设计一个项目button时,我想将b ...

  9. android89 服务service

    #服务 服务不能new,new出来的只是一个普通java对象不是服务,只能够通过Intent和startService(intent)创建服务. ###开启方式 * startService,onCr ...

  10. 在cocos2d-x中使用位图字体

    http://blog.csdn.net/fansongy/article/details/9006677 通常情况下,游戏中绚丽的文字和数字都不是字体生成的而是"贴"上去!今天, ...