带有表名的 MySql 联合查询 [英] MySql union query with table name

查看:71
本文介绍了带有表名的 MySql 联合查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个查询来从两个表中获取数据.我正在使用 UNION 并且运行良好.但是我怎么能识别哪一行属于哪个表呢?我想要这样的数据.

I have to write one query that will fetch data from two table. I am using UNION and working perfectly. But how can i recognize which row belongs to which table? i want data like this.

==================================================
          Table Name     | Person Name
==================================================
          table one      | xyz
--------------------------------------------------
          table two      | abc
--------------------------------------------------
          table one      | www

推荐答案

向您的查询添加额外的列,例如

Add an extra column to your query like

select col1,col2,...,coln,'Table1' as Tablename from table1
Union
select col1,col2,...,coln,'Table2' as Tablename from table2

这篇关于带有表名的 MySql 联合查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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