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


mysql php subtract 2 fields from 2 tables Improving Query

嗨,伙计们,我正在努力改进我的查询以获得更好的性能,是否有可能以更好的方式编写此查询,非常感谢您的帮助

$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>";
}

使用连接将 SQL 更改为一个查询,并在查询中执行减法:

$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'";