简体   繁体   English

如何使用MySQL在一个查询中查询两个不同的数据库?

[英]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.

相关问题 如何使用MySQL在单个查询中运行两个不同的查询? - how to run two different queries in a single query using MySQL? mysql连接查询与两个不同数据库中的表 - mysql join query with tables in two different databases 如何在使用两个不同数据库的 BD Query Laravel 中使用 Join? - How to use Join in BD Query Laravel using two different databases? 我如何使用pdo php扩展名在使用联接的mysql中进行查询(来自不同数据库的2个表)? - how can i use pdo the php extension to make a query in mysql that uses a join (2 tables from different databases)? 我如何在同一时间连接到两个数据库并在mysql中查询数据 - How can connect to two databases in same time and query data in mysql, I have same host 我如何在mysql中查询两个不同的表 - How can i query two different tables in mysql 使用mysql在单个查询中从两个不同的表中选择 - select from two different tables in single query using mysql 如何使用单个查询执行多个算术运算并将答案存储在同一表的两个不同列中 - How can I do more than one arithmetic operation using a single query and store the answer in two different columns on the same table 如何使用mysqli查询连接来自两个不同数据库的两个表? - How to join two tables from two different databases using mysqli query? 如何从同一个表中的单个 MySQL 查询中获取两组结果? - How can I fetch two sets of results in a single MySQL query from the same table?
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM