sql table how to multliply array of numbers fetche

2019-03-04 11:13发布

问题:

In a sql table how to multiply array of numbers fetched from sql table

Table like given below

Id         country       Person        Money

1           UK           john          2010
2           USA          Henry         120
3           RUS          neko          130
4           GER          suka          110
7           CAN          beater        1450
8           USA          lusi          2501

coding

SELECT Money  
FROM Customers;

how to get a money column separate rows only and multiply with given number and stored into array return back

$number = 2;

$Arratotal =
(2010*$number,120*$number,110*$number,1450*$number,2510*$number)

return $Arratotal;

回答1:

Assuming $myDatabase is a mySQLi connection.

This code is used to get money multiplied by a number.

$myNumber = 2;
$qry = "SELECT (Money * {$myNumber}) AS money
    FROM Customers";
$result = $myDatabase->query($qry, MYSQLI_STORE_RESULT);

And this code is to store all money in array $arrayTotal.

while ($row = $result->fetch_object()) {
    $arrayTotal[] = $row->money;
}

Hope this help.



回答2:

Why don't you try like this?

DECLARE @number int=2
SELECT Money * @number AS Money 
FROM Customers;

And calculate sum form code. Otherwise try this

DECLARE @number int=2
SELECT SUM(Money * @number) AS total
FROM Customers;


回答3:

If you need to update the rows in the db instead of just getting the new value out, you can also do this in the database and it will be much faster than getting the data out and putting it back in.

UPDATE Customers SET Money = Money * 2