[英]Join Two Different Table
我試着減去2個不同的表相同的“pocde”欄目。 我已經指出下面的代碼,但它並沒有為我工作。 任何想法?
$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>';
}
由於分組依據,您無法執行此查詢。 您可以這樣更改查詢:
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)
PHP代碼中有錯誤,因為$row = mysql_fetch_array($result)
創建一個數字數組。 您將不得不使用$row[0]
, $row[1]
等。 如果想在代碼中使用關聯數組( $row['totals']
等),則必須使用
mysql_fetch_array($result, MYSQL_ASSOC)
或者你用
mysql_fetch_assoc($result)
。 請注意,兩者均已棄用,您應該真正使用PDO。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.