dsh102123 2013-08-13 19:45
浏览 65
已采纳

将6个查询合并为1个结果集以进行导出?

Ok, so this may sound a little strange and maybe over complicated. Here is the situation. I 2 sets of 3 queries. I will try to make a simple example to explain exactly what I am trying to do:

Queries:

//First set of queries
$query1 = "SELECT Name, Date FROM Table1";
$query2 = "SELECT Type, Place, Location FROM Table2";
$query3 = "SELECT One FROM Table3";

//Second set of queries
$query4 = "SELECT Name, Date FROM Table1 WHERE ID=1";
$query5 = "SELECT Type, Place, Location FROM Table2 WHERE ID=1";
$query6 = "SELECT One FROM Table3 WHERE ID=1";

You just have to trust me when I tell you that I CANNOT combine these two sets of queries. these are over simplified select statements to get the concept of what I am trying to do.

So here is my php code:

//Set 1
$data1 = mysql_query($query1) or die(mysql_error());
$data2 = mysql_query($query2) or die(mysql_error());
$data3 = mysql_query($query3) or die(mysql_error());
while ($line1 = mysql_fetch_array($data1, MYSQL_ASSOC) &&
        $line2 = mysql_fetch_array($data2, MYSQL_ASSOC)) {
    while ($line3 = mysql_fetch_array($data3, MYSQL_ASSOC)) {
        //COMBINE $line1, line2, line3 into a single $lineSet1 -- HOW DO I DO THIS?
    }
}

//Set 2
$data4 = mysql_query($query4) or die(mysql_error());
$data5 = mysql_query($query5) or die(mysql_error());
$data6 = mysql_query($query6) or die(mysql_error());
while ($line4 = mysql_fetch_array($data4, MYSQL_ASSOC) &&
        $line5 = mysql_fetch_array($data5, MYSQL_ASSOC)) {
    while ($line6 = mysql_fetch_array($data6, MYSQL_ASSOC)) {
        //COMBINE $line4, line5, line6 into a single $lineSet2 -- HOW DO I DO THIS?
    }
}

//Append $lineset1 and $lineset2 so I have 1 resultset $results
$result = array_merge($lineSet1, $lineSet2);
//So now I can pass this $result array into my array2csv function that takes a multidimensional array:
function array2csv(array &$array)
{
   if (count($array) == 0) {
     return null;
   }
   ob_start();
   $df = fopen("exportedLeads{$_SESSION['id']}.csv", 'w');
   fputcsv($df, array_keys(reset($array)));
   foreach ($array as $row) {
      fputcsv($df, $row);
   }
   fclose($df);
   return ob_get_clean();
}

I know this seems really complicated, but I am pretty confused and not that good at php. Any help would be appreciated. Thanks!

TABLE1:

ID | Name | Date

TABLE2:

ID | Table1_ID | Type | Place | Location

TABLE3:

ID | Table1_ID | One

EDIT: I have been reading into JOIN statements. Is this possible a case for that?

  • 写回答

2条回答 默认 最新

  • dop83362 2013-08-13 20:02
    关注

    You can resume both of your sets into a single query like using JOIN assuming your ID's match.

    First set into 1 query:

    SELECT t1.Name, t1.Date, t2.Type, t2.Place, t3.One FROM Table1 t1
      JOIN Table2 t2
        ON t2.Table1_ID = t1.ID
      JOIN Table3 t3
        ON t3.Table1_ID = t1.ID
    

    Second set into 1 query:

    SELECT t1.Name, t1.Date, t2.Type, t2.Place, T2.Location, t3.One
      FROM Table1 t1
      JOIN Table2 t2
        ON t2.Table1_ID = t1.ID
      JOIN Table3 t3
        ON t3.Table1_ID = t1.ID
     WHERE t1.ID = 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题