Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
UNION当我使用命令搜索多个表时,如何选择每个结果来自哪个表?
UNION
例如,如果两个表都有结果,我如何添加一个列来说明(或区分)它是来自 tableA 还是 tableB。
试试这个,只需为表名添加一个虚拟列。
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'