商业广告语,凤凰劫七夫乱后宫,黑暗圣经1
1.join和union区别
columna |
columnb |
columnc |
x1 |
y1 |
z1 |
x2 |
y2 |
z2 |
x3 |
y3 |
z3 |
表1.2 table2数据库表
columna |
columnd |
columne |
x1 |
d1 |
e1 |
x2 |
d2 |
e2 |
x3 |
d3 |
e3 |
table1和table2表共有的列为columna,如果通过columna列的值连接table1和table2两个表,即连接条件为table1.columna=table2.columna,此时得到的连接结果如表1.3所示。
表1.3 连接table1和table2表
columna |
columnb |
columnc |
columnd |
columne |
x1 |
y1 |
z1 |
d1 |
e1 |
x2 |
y2 |
z2 |
d2 |
e2 |
x3 |
y3 |
z3 |
d3 |
e3 |
上述连接过程的实现代码可表示如下:select * from table1 join table2 on table1.columna=table2.columna
实例2 典型的二表记录的union运算
假定有两个表table3和table4,其包含的列和数据分别如表2.1和表2.2所示。
表2.1 table3数据库表
columna |
columnb |
columnc |
x1 |
y1 |
z1 |
x2 |
y2 |
z2 |
x3 |
y3 |
z3 |
表2.2 table4数据库表、
columna |
columnd |
columne |
x4 |
y4 |
z4 |
x5 |
y5 |
z5 |
x6 |
y6 |
z6 |
table3表和table4表具有相同的列结构,列数也要相同,列名可以不同,以第一个表的列名为新表的列名,因此可以使用union运算符连接两个表的记录集,得到的连接结果如表2.3所示。
表2.3 使用union连接table3表和table4表的记录
columna |
columnb |
columnc |
x1 |
y1 |
z1 |
x2 |
y2 |
z2 |
x3 |
y3 |
z3 |
x4 |
y4 |
z4 |
x5 |
y5 |
z5 |
x6 |
y6 |
z6 |
上述连接过程的实现代码可表示如下:select * from table3 union select *from table4
对比实例1和实例2,不难发现二者的区别。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
数据库SQL---数据库、基本表、视图、索引的定义、修改、删除
在 Azure CentOS VM 中配置 SQL Server 2019 AG - (上)
在 Azure CentOS VM 中配置 SQL Server 2019 AG - (下)
网友评论