我想在查询结果中获取表名。我试图做的查询涉及多个表。这就是为什么我试图获得一个带有结果的列,该结果将指示从哪个表中获取数据。这是我正在使用的代码
(SELECT DISTINCT column_1 as keywords from table_1)
UNION ALL
(SELECT DISTINCT column_2 as keywords from table_2)
答案 0 :(得分:6)
使用此:
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
,其中包含原始表名。