dslijian2015 2018-05-20 18:23
浏览 48
已采纳

如何使用一个查询从多个数据库下载所有变量?

I have several tables in which a shared variable is stored. How to get all variables from each table with one query? And how to return all variables as in the example? Is it even possible?

$id1 = BDR::selectBySQL("g","SELECT * FROM i1 WHERE ix='".$this->ixx."' LIMIT 1");
foreach($id1 as $id1) 
{
    $id1 = $id1['idz'];

}
$id2 = BDR::selectBySQL("g","SELECT * FROM i2 WHERE ix='".$this->ixx."' LIMIT 1");
foreach($id2 as $id2) 
{
    $id2 = $id2['idxc'];

}
$id3 = BDR::selectBySQL("g","SELECT * FROM i3 WHERE ix='".$this->ixx."' LIMIT 1");
foreach($id3 as $id3) 
{
    $id3 = $id3['idsd'];

}
return ['id2'=>$id1,'id2'=>$id2,'id3'=>$id3];
  • 写回答

1条回答 默认 最新

  • douwei8911 2018-05-20 18:28
    关注

    If the table have the same schema (same of columnd and corresponding data type) you could use a select union all

      SELECT * FROM i1 WHERE ix='".$this->ixx."' LIMIT 1 
      union all  
      SELECT * FROM i2 WHERE ix='".$this->ixx."' LIMIT 1
      union all 
      SELECT * FROM i3 WHERE ix='".$this->ixx."' LIMIT 1 
    

    In this way you should retrieve a list of rows .. with the desired result and iterating over the result you could obation all the values you need

    $idAll = BDR::selectBySQL("g",
        "SELECT * FROM i1 WHERE ix='".$this->ixx."' LIMIT 1 
          union all  
          SELECT * FROM i2 WHERE ix='".$this->ixx."' LIMIT 1
          union all 
          SELECT * FROM i3 WHERE ix='".$this->ixx."' LIMIT 1 ");
    
    foreach($idAll as $row) 
    {
        $id[] = $row['idz'];
    
    }
    
    return $id;
    

    you can check the content of $id Array using var_dump($id);

    and last if the tables comes form different database on same server you can use an explictit table name eg: db1.i1

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。