如何将不同表中的2列乘以相同的数据库mysqli php

时间:2018-03-27 09:27:14

标签: php mysql multiplying

<?php

$con = mysqli_connect("localhost", "root", "", "zoo");

if (mysqli_connect_error()) {
    echo "Failed to connect" . mysqli_connect_error();
}

$sel = "Select * from animal where quantity";

$result = mysqli_query($con, $sel);

while ($row = mysqli_fetch_array($result)) {

    echo "<tr>";
    echo"<td>" . $row['quantity'] . "</td>";
}
$qsel = "Select * from price where bill";
$result = mysqli_query($con, $qsel);
while ($row = mysqli_fetch_array($result)) {

    echo "<tr>";
    echo "<td>" . $row['bill'] . "</td>";
}
?>

如何将不同表中的2列乘以相同的数据库mysqli php

2 个答案:

答案 0 :(得分:0)

试试这个它会给你整个结果而无需在php中进行操作。

select (quantity* bill) as total from animal inner join price on animal.id=price.animal_id group by price.animal_id;

答案 1 :(得分:0)

$con = mysqli_connect("localhost", "root", "", "zoo");

if (mysqli_connect_error()) {
    echo "Failed to connect" . mysqli_connect_error();
}

$sel = "select (quantity* bill) as total from animal inner join price on animal.id=price.animal_id group by price.animal_id";
$result = mysqli_query($con, $sel);
while ($row = mysqli_fetch_array($result)) {

    echo "<tr>";
    echo"<td>" . $row['total'] . "</td>";
}

在复制和粘贴之前检查您的表关系列名称并相应地更新。