来自多个数据库表的MySQL联合 [英] Mysql union from multiple database tables

查看:108
本文介绍了来自多个数据库表的MySQL联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数据库,这些数据库的表具有相同的模式.因此,我正在此表上执行UNION以显示所有数据库中的记录,效果很好.

I have a group of databases which have a table with same schema. So, i am doing a UNION on this table to show the records from all databases, which works nice.

但是,现在我必须检测哪一行属于哪个数据库/表,因为我需要修改该特定记录.我找到了链接在获取UNION时遇到问题,但无法找到确定行<->表关系的逻辑.

But, now i have to detect which row belongs to which database/table, as i need to modify that particular record. I found this link on getting the UNION, but am unable to find the logic which determines row<->table relationship.

推荐答案

尝试一下:

(SELECT *, 'db1' as DB_NAME from db1..table1)
union
(SELECT *, 'db2' as DB_NAME from db2..table1)
union
(SELECT *, 'db3' as DB_NAME from db3..table1)
...

这篇关于来自多个数据库表的MySQL联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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