用不同的列名堆叠两个 sql 表 (2008)

stacking two sql tables (2008) with different column names(用不同的列名堆叠两个 sql 表 (2008))
本文介绍了用不同的列名堆叠两个 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)