添加列以说明联合结果来自哪个表 [英] add column to say which table a union result is from

查看:43
本文介绍了添加列以说明联合结果来自哪个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I select which table each result is from, when I use a UNION command to search multiple tables?

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

For example, if there are results from both tables, how can I add a column that will say (or differentiate between) whether it is from tableA or tableB.

推荐答案

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

try this one, simply add a virtual column for the name of the table.

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'

这篇关于添加列以说明联合结果来自哪个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆