2

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

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

1 回答 1

7

用这个:

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,其中包含原始表名称。

于 2013-06-10T03:39:31.770 回答