0

UNION当我使用命令搜索多个表时,如何选择每个结果来自哪个表?

例如,如果两个表都有结果,我如何添加一个列来说明(或区分)它是来自 tableA 还是 tableB。

4

1 回答 1

1

试试这个,只需为表名添加一个虚拟列

SELECT *
FROM
(
    SELECT *, 'tableA' as tableName FROM tableA
    UNION ALL
    SELECT *, 'tableB' as tableName FROM tableB
    UNION ALL
    SELECT *, 'tableC' as tableName FROM tableC
) s
WHERE   colName = 'hello'
于 2012-11-28T05:32:56.383 回答