查询如何为每行MySQL乘以2个单元格?

时间:2011-04-17 11:35:08

标签: mysql multiplying

我想为每一行乘以2个单元格,并将其值放在名为Total的最后一列中。这可以通过普通查询来完成吗?

示例:

Pieces | Price | Total
6      |   4   |  null // should be 24
2      |  10   |  null // should be 10

4 个答案:

答案 0 :(得分:67)

使用此:

SELECT 
    Pieces, Price, 
    Pieces * Price as 'Total' 
FROM myTable

答案 1 :(得分:8)

你可以用:

UPDATE mytable SET Total = Pieces * Price;

答案 2 :(得分:6)

我认为这应该有效。这实际上将它放在数据库的列中

UPDATE yourTable yt SET yt.Total = (yt.Pieces * yt.Price)

如果要从数据库中检索2个值并将乘法仅放在结果的第三列中,那么

SELECT yt.Pieces, yt.Price, (yt.Pieces * yt.Price) as 'Total' FROM yourTable yt

将成为你的朋友

答案 3 :(得分:-2)

这是我的解决方案:

我一直在寻找如何显示结果而不是计算......

如此。在这种情况下。数据库中没有TOTAL列,但网页上有一个总计......

 <td><?php echo $row['amount1'] * $row['amount2'] ?></td>

这也是首先需要......

<?php 
   $conn=mysql_connect('localhost','testbla','adminbla');
mysql_select_db("testa",$conn);

$query1 = "select * from info2";
$get=mysql_query($query1);
while($row=mysql_fetch_array($get)){
   ?>