我尝试使用相同的“pocde”列减去2个不同的表。我已经说明了下面的代码,但它对我不起作用。任何的想法?
$result = mysql_query("
SELECT
productlist.*,
SUM(rreturn.total)-SUM(rreturn.total) as totals,
SUM(rsales.tax)-SUM(rreturn.tax) as tax
FROM productlist
LEFT JOIN rsales ON rsales.pcode = productlist.pcode
LEFT JOIN rreturn ON rreturn.pcode = productlist.pcode
GROUP BY pcode
ORDER BY total ASC");
while($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td style = "background-image:url(images/buts1.png)">'.($row['totals'].'</td>';
echo '<td style = "background-image:url(images/buts1.png)">'.($row['tax'].'</td>';
echo '</tr>';
}
答案 0 :(得分:0)
由于分组依据,您无法执行此查询。 您可以像这样更改您的查询:
SELECT pl.*, plagg.totals, plagg.tax from productlist pl inner join ( SELECT productlist.pcode, SUM(rreturn.total)-SUM(rreturn.total) as totals, SUM(rsales.tax)-SUM(rreturn.tax) as tax FROM productlist LEFT JOIN rsales ON rsales.pcode = productlist.pcode LEFT JOIN rreturn ON rreturn.pcode = productlist.pcode GROUP BY productlist.pcode ) plagg on (plagg.pcode = pl.pcode)
答案 1 :(得分:-1)
PHP代码中存在错误,因为$row = mysql_fetch_array($result)
会创建数字数组。您必须使用$row[0]
,$row[1]
等。如果您想尝试在代码中使用关联的数组($row['totals']
等),则必须使用
mysql_fetch_array($result, MYSQL_ASSOC)
或您使用
mysql_fetch_assoc($result)
。请注意,两者都已弃用,您应该使用PDO。