stacking two sql tables (2008) with different column names(用不同的列名堆叠两个 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用不同的列名堆叠两个 sql 表 (2008)
基础教程推荐
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
