选择UNION ALL以将表名显示为附加列(Select with UNION ALL to display tablename as an additional column)

编程入门 行业动态 更新时间:2024-10-23 16:14:25
选择UNION ALL以将表名显示为附加列(Select with UNION ALL to display tablename as an additional column) mysql

对于MSSQL或MySQL,这是一个将两个结果合二为一的查询:

SELECT boy as person from table1 union all SELECT girl as person from table2

如何修改上面的查询,以便结果包含带有表名的第二个(添加的)列(因此它包含table1或table2值)。

For MSSQL or MySQL, here is a query that unites two results into one:

SELECT boy as person from table1 union all SELECT girl as person from table2

How to modify the query above so that the result contains the second (added) column with the name of the table (so it contains table1 or table2 value).

最满意答案

你可以给表格名称作为第二列的字符串横向

SELECT boy as person, 'table1' as column2 from table1 union all SELECT girl as person, 'table2' as column2 from table2

you can give string lateral with table name as second column

SELECT boy as person, 'table1' as column2 from table1 union all SELECT girl as person, 'table2' as column2 from table2

更多推荐

本文发布于:2023-07-15 18:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Select   将表名   UNION   display   column

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!