doukang7486 2012-06-11 16:24
浏览 57

mysql php从2个表中减去2个字段改进查询

hi guys im trying to improve my query for better performance is it possible to write this query in better way thanks a lot your helps

$query = " SELECT A  FROM out_org where zone_id='1'";
$query2 = " SELECT A  FROM out_dis where zone_id='1'";

$result = mysql_query($query);
$result2 = mysql_query($query2);

echo "<table border=1 style='background-color:#F0F8FF;' >";
echo "<caption><EM>my table</EM></caption>";
echo "<tr>";
echo "<th>" .OA. "</th>" ;
echo "<th>" .DA. "</th>";
echo "<th>" .total. "</th>";
echo "</tr>";

while($row = mysql_fetch_array($result)  )
{
    while( $row2 = mysql_fetch_array($result2)){
        echo "<tr>";          
        echo "<td>" .$row['A']."</td>";
        echo "<td>" .$row2['A']."</td>";
        echo "<td>" .$total = $row['A'] - $row2['A']."</td>";         
        echo "</tr>";           
    }
    echo "</table>";
}
  • 写回答

1条回答 默认 最新

  • drgdn82648 2012-06-11 16:28
    关注

    Change your SQL to one query using a join and do the subtraction within the query:

    $query = "SELECT (o1.A - o2.A) as value 
              FROM out_org o1 
                   LEFT JOIN out_dis o2 
                   ON o1.zone_id = o2.zone_id
               WHERE o1.zone_id='1'";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线