在Postgres查询结果中获取表名 [英] get table name in Postgres query result

查看:102
本文介绍了在Postgres查询结果中获取表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在查询结果中获取表名。我试图做的查询涉及多个表。这就是为什么我试图获得一列带有结果的列,该列将指示从哪个表中获取数据。这是我正在使用的代码

I am trying to get table name in query result. The query I am trying to do involves multiple table. This is why I am trying to get a column with result which will indicate from which table the data taken from. Here is the code I am using

(SELECT DISTINCT column_1 as keywords from table_1) 
UNION ALL
(SELECT DISTINCT column_2 as keywords from table_2)


推荐答案

使用此:

SELECT DISTINCT column_1 AS keywords, 'table_1' AS tablename
FROM table_1
UNION ALL
SELECT DISTINCT column_2 AS keywords, 'table_2' AS tablename
FROM table_2

再将一列 tablename 添加到结果集中,其中包含原始表名。

It adds one more column tablename to result set, which contains originating table name.

这篇关于在Postgres查询结果中获取表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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