php开源嘛
首页 | English | MySql数据库 | Php编程 | 建站杂烩 | 系统服务器 | 资源共享 | 专题 | 才桀网 | 支持论坛
  当前位置:主页>MySql数据库>文章内容
MySQL数据库学习:MySQL Join详解
来源:网络 作者:本站整理 发布时间:2007-11-15  

MySQL数据库学习:MySQL Join详解

还是先 Create table 吧 phpma.com

  create table emp(
  id int not null primary key,
  name varchar(10)
  );

  create table emp_dept(
  dept_id varchar(4) not null,
  emp_id int not null,
  emp_name varchar(10),
  primary key (dept_id,emp_id));

  insert into emp() values
  (1,"Dennis-1"),
  (2,"Dennis-2"),
  (3,"Dennis-3"),
  (4,"Dennis-4"),
  (5,"Dennis-5"),phpma.com
  (6,"Dennis-6"),
  (7,"Dennis-7"),
  (8,"Dennis-8"),
  (9,"Dennis-9"),
  (10,"Dennis-10");

  insert into emp_dept() values
  ("R&D",1,"Dennis-1"),
  ("DEv",2,"Dennis-2"),
  ("R&D",3,"Dennis-3"),
  ("Test",4,"Dennis-4"),
  ("Test",5,"Dennis-5");

>> left join
  -------------phpma.com
  select a.id,a.name,b.dept_id
  from emp a left join emp_dept b on (a.id=b.emp_id);

  # 挑出左边的 table emp 中的所有资料,即使 emp_dept 中没有的资料也挑出来,没有的就用 NULL   来显示,
  # 也即显示资料是以左边的 table emp 中的资料为基础

  mysql> select a.id,a.name,b.dept_id
  -> from emp a left join emp_dept b on (a.id=b.emp_id);
  +----+-----------+---------+
  | id | name      | dept_id |
  +----+-----------+---------+
  |  1 | Dennis-1  | R&D     |
  |  2 | Dennis-2  | DEv     |
  |  3 | Dennis-3  | R&D     |
  |  4 | Dennis-4  | Test    |
  |  5 | Dennis-5  | Test    |
  |  6 | Dennis-6  | NULL    |
  |  7 | Dennis-7  | NULL    |
  |  8 | Dennis-8  | NULL    |
  |  9 | Dennis-9  | NULL    |
  | 10 | Dennis-10 | NULL    |
  +----+-----------+---------+

  # 挑出 table emp 中有而 table emp_dept 中没有的人员资料
  select a.id,a.name,b.dept_idphpma.com
  from emp a left join emp_dept b on (a.id=b.emp_id)
  where b.dept_id IS NULL;

(阅读次数:

共3页: 上一页 1 [2] [3] 下一页
上一篇:Phpma学习PHP高级技巧篇(二)...   下一篇:Phpma学习PHP高级技巧篇(三)...
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·用于SELECT和WHERE子句的SQL函数
·MySQL索引分析和优化
·MySQL数据库下.frm .MYD .MYI损
·怎么删除MySQL数据库中的重复数
·MySQL数据导入与导出
·MySQL数据库中SQL查询语句精华
·如何优化MySQL数据库性能
·MySQL查询优化之查询优化器
·怎么备份MySQL数据库
·如何导入导出*.sql文件到MySQL数
·MySQL数据导入导出方法与工具介
·MySQL索引分析和优化加速网站访
  相关文章
·如何使MySQL数据库完全中文化呢
·数据库基础:SQL导出到MYSQL数据
·保护MySQL数据库中的重要数据的
·Phpma学习MySQL数据库数据的备份
·MySQL和SQL Server的安全性分析
·尝试提高MySQL查询效率的方法
·MySQL数据库中文模糊检索问题的
·MySQL数据库中与浮点比较有关的
·转移数据:MySQL数据导入到Sql Se
·用数据库命令对MySQL数据库进行
·MySQL数据库高级特性--对比其他
·优化mysql数据库性能的十个参数.

关于我们 | 本站声明 | 友情连接 | 诚邀加盟 | 网站地图
版权Power by DedeCms   技术支持QQ =>> 罗江游鱼   Jacking  后台登陆
Copyright @ 2007 公司地址:柳州市东环路268号 邮编:545006 电话:15920389818 桂ICP备07006725号