查询如何为每行 MySQL 乘以 2 个单元格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5693259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can a query multiply 2 cell for each row MySQL?
提问by Arne Nouwynck
I want to multiply 2 cells for each row and put the value of that in the last column called Total. Can this be done by a normal query?
我想为每行乘以 2 个单元格,并将其值放在名为 Total 的最后一列中。这可以通过普通查询来完成吗?
Example:
例子:
Pieces | Price | Total
6 | 4 | null // should be 24
2 | 10 | null // should be 10
回答by Prescott
Use this:
用这个:
SELECT
Pieces, Price,
Pieces * Price as 'Total'
FROM myTable
回答by vbence
You can do it with:
你可以这样做:
UPDATE mytable SET Total = Pieces * Price;
回答by Nanne
I'm assuming this should work. This will actually put it in the column in your database
我假设这应该有效。这实际上会将它放在数据库中的列中
UPDATE yourTable yt SET yt.Total = (yt.Pieces * yt.Price)
If you want to retrieve the 2 values from the database and put your multiplication in the third column of the result only, then
如果您想从数据库中检索 2 个值并将乘法仅放在结果的第三列中,则
SELECT yt.Pieces, yt.Price, (yt.Pieces * yt.Price) as 'Total' FROM yourTable yt
will be your friend
会是你的朋友
回答by tijnn
this was my solution:
这是我的解决方案:
i was looking for how to display the result not to calculate...
我正在寻找如何显示结果而不是计算...
so. in this case. there is no column TOTAL in the database, but there is a total on the webpage...
所以。在这种情况下。数据库中没有TOTAL列,但是网页上有总计...
<td><?php echo $row['amount1'] * $row['amount2'] ?></td>
also this was needed first...
这也是首先需要的......
<?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)){
?>

