[英]How can I query two different databases in a single query using MySQL?
I have two websites that post news information. 我有两个发布新闻信息的网站。 I just got a request to make a single page for one of the sites that shows the mixed results from both sites. 我刚收到一个请求,要求为其中一个站点制作一个页面,以显示两个站点的混合结果。 They are located on the same server and the tables are close to identical, but I am having problems figuring out how to combine the two queries. 它们位于同一台服务器上,并且表几乎相同,但是在弄清楚如何组合两个查询时遇到了问题。 The postings need to be sorted by date, title when all is said and done. 说完所有内容后,需要按日期,标题对发布进行排序。
For arguments sake, a simplified form of the tables is 出于参数考虑,表的简化形式是
DB 1, TABLE Clips1 (PK_ID, DateToShow, Title, Description) DB 1,TABLE Clips1(PK_ID,DateToShow,Title,Description)
DB 2, TABLE Clips2 (PK_ID, DateToShow, Title, Description) DB 2,TABLE Clips2(PK_ID,DateToShow,标题,说明)
So the single query would hit both databases (usernames and passwords are different currently), or two queries and some kind of combination that would keep the date and titles sorted. 因此,单个查询将同时击中两个数据库(当前用户名和密码不同),或两个查询以及某种组合,从而使日期和标题保持排序。
IF you can use a single connection for the query, it is trivial. 如果您可以对查询使用单个连接,则它是微不足道的。
You say that the schemas are on the same server, so with the appropriate permissions this shouldn't be a problem. 您说模式在同一台服务器上,因此具有适当的权限应该不会有问题。
The general syntax for this is: 通用语法是:
SELECT schemaA.table.column
FROM schemaA.table
UNION
SELECT schemaB.table.column
FROM schemaB.table
You can even do JOINs, just remember to specify the appropriate schema for every table and field reference. 您甚至可以执行JOIN,只记得为每个表和字段引用指定适当的架构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.