[英]run a single query on multiple databases
1)I have a table employee(emp_id, emp_name,emp_salary); 1)我有一个表employee(emp_id,emp_name,emp_salary); I want to find top 2 employees having max salary without using limit 我想找到薪水最高的前2名员工而没有使用限制
2)how do i run a query so that it takes records from databses on two database mysql servers this should be done in single mysql query 2)我如何运行查询,以便它从两个数据库mysql服务器上的数据库中获取记录,这应该在单个mysql查询中完成
thanks in advance for any help or replies 在此先感谢您的帮助或答复
A single query can't talk to two database servers at once. 一个查询不能一次与两个数据库服务器对话。 It's just not allowed. 只是不允许。 But you can link the two servers using a FEDERATED table. 但是您可以使用FEDERATED表链接两个服务器。 This makes the "remote" table appear as if it's really stored locally. 这使“远程”表看起来像是真正存储在本地。
However, then you're still stuck to having to use a union query, unless the two tables can be JOINed somehow. 但是,除非仍然可以以某种方式联接两个表,否则您仍然必须使用联合查询。
SELECT blah,blah,blah
FROM localtable
UNION
SELECT blah,blah,blah
FROM federatedtable
So what exactly is wrong with the LIMIT? 那么LIMIT到底有什么问题呢?
select *
from (select *
from db1.employee
order by salary desc limit 2
union
select *
from db2.employee
order by salary desc limit 2
) emp
order by emp.salary desc limit 2
if employees in bd1 and db2 are certainly different or you dont want to remove duplicates change to UNION ALL
如果bd1和db2中的员工肯定不同,或者您不想删除重复项,请更改为UNION ALL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.