php – 跨服务器的Mysql查询,不使用联合表

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 跨服务器的Mysql查询,不使用联合表脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我将我的数据库的一些表移动到另一台服务器并且不想使用
联邦表有很多原因(性能是主要原因).所以我必须在我的 PHP类中创建2个不同的连接
并在不同服务器的表之间连接的代码中重写我的查询.

例如,我有两个表:位于不同服务器中的用户和企业.

当它在同一台服务器上时,查询是:

select name From Enterprise E 
inner join Users U on E.cod = U.cod
where E.cod = $my_code and U.codUsers in ($users);

所以我改为

$rst= select group_concat(U.cod) from Users as U 
where U.codUsers in ($users)

select name from EnterPRise E
where E.cod = $mycode and E.cod in ($rst);

我的问题是,当我有这种类型的查询时,我怎样才能做到一点

select e.name,e.datebegin,e.dateend from Enterprise E
leftjoin ( select h.callQuantITy,h.history from thirdtable 
       inner join Users u on e.cod = u.cod
       where u.codHistory in (1,2,3)
           group by u.cod)

我的问题很明确?对不起我的英语不好

首先,联合表是更好的解决方案(它们是针对您尝试手动完成的那种方式而制作的).但你不想要它们,所以这是下一个最好的事情:

对于比第一个示例更复杂的事情,您应该通过插入表格内容而不是表格来手动模拟远程表格.

我将以这种方式重写你的第一个例子,因为你的第二个例子搞砸了,我甚至不知道你想要在那里表达什么.

使用1台服务器时,您有以下代码

select name from Enterprise E 
inner join Users U on E.cod = U.cod
where E.cod = $my_code and U.codUsers in ($users);

您现在可以将表替换为表的实际数据:

select name from Enterprise E 
inner join 
   (      select 1 as cod,4 as codUsers,20 as codHistory
    union select 2 as cod,8 as codUsers,500 as codHistory
    union select 3 as cod,29 as codUsers,100 as codHistory
   ) as U
on E.cod = U.cod
where E.cod = $my_code and U.codUsers in ($users);

要做到这一点,你必须将表数据构建为一个字符串(我在这里使用pdo):

foreach($db->query('select * from Users U where U.codUsers in ($users)') as $row) {
    if($data !== '') { $data .= 'union '; }
    $data .= 'select ' . $row['cod'] . ' as cod,' 
             . $row['codUsers'] . ' as codUsers,' 
             . $row['codHistory'] . ' as codHistory '; 
}

当然,你必须使它适合你的Users-table的布局(并且不要忘记一些’用于字符串列),并且你可以省略你不需要的列.

您现在可以将该字符串放在您拥有Users-table之前的任何位置,并将您的代码编写为在1台服务器上,因此您的第一个代码看起来像

select name from Enterprise E 
inner join ($data) as U on E.cod = U.cod
where E.cod = $my_code and U.codUsers in ($users);

和你的第二个代码(虽然我提醒你,它开始时是破碎的,也不会在1台服务器上运行)看起来像

select e.name,h.history from thirdtable 
       inner join ($data) as u on e.cod = u.cod
       where u.codHistory in (1,3)
           group by u.cod)

你应该确保只有少量用户进入你的$data-string,然后这将正常工作.否则,您确实需要联合表.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 跨服务器的Mysql查询,不使用联合表全部内容,希望文章能够帮你解决php – 跨服务器的Mysql查询,不使用联合表所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。