How can a query multiply 2 cell for each row MySQL?

前端 未结 4 1801
囚心锁ツ
囚心锁ツ 2020-12-04 01:46

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?

Example:

Piec         


        
相关标签:
4条回答
  • 2020-12-04 01:53

    You can do it with:

    UPDATE mytable SET Total = Pieces * Price;
    
    0 讨论(0)
  • 2020-12-04 01:56

    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...

     <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)){
       ?>
    
    0 讨论(0)
  • 2020-12-04 02:00

    Use this:

    SELECT 
        Pieces, Price, 
        Pieces * Price as 'Total' 
    FROM myTable
    
    0 讨论(0)
  • 2020-12-04 02:05

    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

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

    will be your friend

    0 讨论(0)
提交回复
热议问题