用不同的列名堆叠两个 sql 表 (2008) [英] stacking two sql tables (2008) with different column names

查看:38
本文介绍了用不同的列名堆叠两个 sql 表 (2008)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查了这个站点的代码来堆叠两个表(将一个表的结果放在另一个表的结果下方),其中列名不同.我知道当名称相同时 UNION ALL 有效..而且我知道 UNION ALL 对一个表中缺少的列名进行分配时,当一个表比另一个表具有更多信息时会起作用..但是如果该列会怎样名字不一样?就像如果在一张表中的列名称是 CHP 而在另一张表中它是CHILD HEALTH PLUS"并且我需要将这两列堆叠在一起会怎样?

I checked this site for code to stack two tables (put the results from one table beneath the results from another) where the column names are different. I know that UNION ALL works when the names are the same.. and I KNOW THAT UNION ALL with an assignment for the column names that are missing from one table works when one table has more info than the other.. but what if the column names are different? like what if in one table the column name is CHP and in another it is "CHILD HEALTH PLUS" and I need those two columns to be stacked on top of one another?

推荐答案

只要列的数据类型相同,就可以使用 UNION.列名是否不同并不重要.

A UNION can be used as long as the datatypes of the columns are the same. It doesn't matter if the column names are different.

SELECT column1
FROM Table1
UNION
SELECT column1
FROM Table2

如果你想知道记录来自哪个表,那么你可以添加另一个字段来区分行:

If you want to know which table the records are coming from, then you can add another field that will distinguish the rows:

SELECT column1, 'Table1' as TableName
FROM Table1
UNION
SELECT column1, 'Table2' as TableName
FROM Table2

这篇关于用不同的列名堆叠两个 sql 表 (2008)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆