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

Arn*_*nck 33 mysql multiplying

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

例:

Pieces | Price | Total
6      |   4   |  null // should be 24
2      |  10   |  null // should be 10
Run Code Online (Sandbox Code Playgroud)

Pre*_*ott 75

用这个:

SELECT 
    Pieces, Price, 
    Pieces * Price as 'Total' 
FROM myTable
Run Code Online (Sandbox Code Playgroud)

  • ROUND(Pieces*Price,2)为`Total` (4认同)

vbe*_*nce 8

你可以这样做:

UPDATE mytable SET Total = Pieces * Price;
Run Code Online (Sandbox Code Playgroud)


Nan*_*nne 6

我认为这应该工作.这实际上将它放在数据库的列中

UPDATE yourTable yt SET yt.Total = (yt.Pieces * yt.Price)
Run Code Online (Sandbox Code Playgroud)

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

SELECT yt.Pieces, yt.Price, (yt.Pieces * yt.Price) as 'Total' FROM yourTable yt
Run Code Online (Sandbox Code Playgroud)

将是你的朋友