如何使用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 2, TABLE Clips2 (PK_ID, DateToShow, Title, Description)
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.
我有两个网站发布新闻信息。 我刚收到一个请求,要求为其中一个站点制作一个页面,显示两个站点的混合结果。 它们位于同一台服务器上,并且表格几乎完全相同,但我在弄清楚如何组合这两个查询时遇到了问题。 当完成所有操作时,帖子需要按日期,标题进行排序。 p>
为了论证,表的简化形式是 p>
DB 1,TABLE Clips1(PK_ID,DateToShow,Title,Description) p>
DB 2,TABLE Clips2(PK_ID,DateToShow,Title,Description) p>
因此,单个查询将同时命中两个数据库(用户名和密码当前不同),或者两个查询以及某种组合可以保持日期和标题的排序。 p> div>
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.