Get table name from Postgres query

I am trying to get the table name as a result of a query. The query I'm trying to do includes several tables. This is why I am trying to get a column with a result that indicates which table the data is taken from. Here is the code I'm using

(SELECT DISTINCT column_1 as keywords from table_1) UNION ALL (SELECT DISTINCT column_2 as keywords from table_2) 
0
source share
1 answer

Use this:

 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 

It adds another tablename column to the result set, which contains the name of the source table.

+5
source

Source: https://habr.com/ru/post/971783/


All Articles