Home > Software engineering >  Mysql Modifying the value by Addition method with Multiple Rows group by _id
Mysql Modifying the value by Addition method with Multiple Rows group by _id

Time:08-22

Hi guys I just want to ask a simple question coz I know there's a way to do this I just don't know the exact keyword to search in Google. Its about MySql Table that has a column Basically,

Price: 50,000 (From Another Table)

-- deposit--     balance--id--    nid--
|   5,000   |   45,000|   1 |      2|
|   2,000   |   43,000|   2 |      2|

What I want is If ever I adjust the Price to lets say; 65,000 Data in balance should update accordingly like this;

-- deposit--      balance-- id--    nid--
 |   5,000   |     60,000|   1 |      2| 
 |   2,000   |     58,000|   2 |      2|

to select the multiple rows I use "nid" but I don't know how I could update all row containing the same nid. My Logic is I want it like this

initialprice=50,000;
newprice=65,000;
additional=15,000;
loop foreach Row -> (balance additional)

Can you guys help me? I've been searching for "addition in column in mysql" and found no luck.

CodePudding user response:

This can be accomplished with an UPDATE statement:

UPDATE your_table
SET balance = balance   15000
WHERE nid = 2;

The "additional" (i.e., 15000) and "nid" (i.e., 2) values could be parameterised to call this from C#:

using var command = new MySqlCommand("UPDATE your_table SET balance = balance   @additional WHERE nid = @nid", connection);
command.Parameters.AddWithValue("@additional", 15000);
command.Parameters.AddWithValue("@nid", 2);
command.ExecuteNonQuery();
  • Related