SQL 表连接

横列拼接

连接查询中用来连接连个表的条件称为连接条件或连接谓词。其形式为:

[<表1>].<列名1><连接运算符>[<表2>].<列2>  

  

     例如:

select (select sname from student where student.Sno=Score.Sno),Degree from Score     以数据多的表为主    sname列是student表   degree是score表的   

 

或者

select sname,degree from student,scor where student.sno=score.sno        sno在这两个表有关联   列名不一定一样 但是数据类型一样 有关系

假设两个表都有同一列的列名   前面一定标明哪一个表的

例如  select  student.sname,  score.degree from student,score  where  student.sno=score.sno 

 竖列拼接

    union是一个特殊的运算符,用于将两个或两个以上的查询产生一个结果集。join将信息水平连接(添加更多列),而union将信息垂直连接(添加更多行)。

   当使用union处理查询时,要注意以下几个关键点。

   (1)、所有union的查询必须在select列表中有相同的列数。

         (2)、查询中对应的列的数据类型必须隐式一致。注意不要求完全一致,只需要隐式一致。

 

 上面一个是学生student表一个是老师teacher表

 有编号sno和tno   sname和tname   ssex 和tsex  类型一致

竖列拼起来是

select sno,sname,ssex,sbirthday from student
union
select tno,tname,tsex,tbirthday from teacher

原文地址:https://www.cnblogs.com/zhangwei99com/p/6557368.html