数据库 sql 表连接

  表链接 分为 横向链接   和纵向链接

 

 

横向链接

   select * from student,score ——笛卡尔积

    查询所有表 会出现 笛卡尔积  就是所有匹配的结果都会展示一遍

   为防止以上情况 就会加上筛选条件

 用表连接时候 要注意  用多的表包含少的表

  如上题  

1;select * from student,score where studene.sno= score.sno

在后面加上表连接的条件 就不会出现笛卡尔积

2;第二种方法使用子查询的方法

select cno, (select sname from student where student.sno=score.sno),degree from score

将要查询的列名用子查询的方法查出来

3  用join on 来链接

select student.sno ,sname,cno,degree  from student join score on student.sno=score.sno

//inner join(默认) //left join(以左表为主表)  //right join(以右表为主表)

4   多个表链接  在 where 后面加and 即可  

     也可以在join 后面加join

 

纵链接    数据类型要一致

 

两个表之间用 union

 

 select tname 名字,tsex 性别,tbirthday 生日 from teacher
 union
 select sname ,ssex,sbirthday from student

 

posted @ 2017-03-16 11:47  v587yy  阅读(182)  评论(0编辑  收藏  举报