简体   繁体   English

合并来自两个不同数据库的相同SQL语句

[英]Merging the same SQL statement from two different databases

I have a mySQL SELECT statement that I run twice, once on each database. 我有一个mySQL SELECT语句,该语句运行两次,每个数据库一次。 They perform the same action but I just need to change the database between the statements. 它们执行相同的操作,但是我只需要在语句之间更改数据库。

This is how it looks like: 它是这样的:

   mysql_select_db("db1");
   mysql_query("SELECT results from table1 where date='2012-01-01'");
   mysql_select_db("db2");
   mysql_query("SELECT results from table1 where date='2012-01-01'");

Is there anyway to simplify this into one statement? 是否有将其简化为一个陈述的方法?

You can reference multiple databases in the same MySQL statement, and you can combine your two queries into a UNION : 您可以在同一个MySQL语句中引用多个数据库,并且可以将两个查询合并为一个UNION

SELECT results from db1.table1 where date='2012-01-01'
UNION ALL
SELECT results from db2.table1 where date='2012-01-01';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM