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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动