Php 连接两个不同的表
我试着用相同的pocde列减去两个不同的表。我已经说明了下面的代码,但它不适合我。有什么想法吗Php 连接两个不同的表,php,mysql,Php,Mysql,我试着用相同的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 r
$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>';
}
由于group by,无法执行此查询。 您可以这样更改查询: 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\u fetch\u array$result创建了一个数字数组。您必须使用$row[0]、$row[1]等等。如果您想在代码$row['totals']等中使用关联数组,则必须使用 mysql\u fetch\u数组$result,mysql\u ASSOC 或者你用 mysql\获取\关联$result
。请注意,两者都已弃用,您应该真正使用PDO。sumreturn.total-sumreturn.total是否始终返回0?否则,您可能必须解释实际问题是什么。什么不起作用?使用带有mysqli_uuu前缀的函数。那些使用mysql的都不推荐使用。它怎么会不起作用呢?有错误吗?如果有任何问题,请发布它们。total应该是rsales.totalit walways返回Null我很好奇pl是什么,先生?我也很好奇plag是什么,先生抱歉,我只是新的t phpIt的子查询名称。但这是SQL而不是PHP