Updating a MySQL table row column by appending a value from user defined variable?


Let us first create a table −

mysql> create table DemoTable1349
   -> (
   -> ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> ProductPrice int
   -> );
Query OK, 0 rows affected (0.71 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1349(ProductPrice) values(7644);
Query OK, 1 row affected (0.28 sec)
mysql> insert into DemoTable1349(ProductPrice) values(90843);
Query OK, 1 row affected (0.06 sec)
mysql> insert into DemoTable1349(ProductPrice) values(9083);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1349(ProductPrice) values(10000);
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1349;

This will produce the following output −

+-----------+--------------+
| ProductId | ProductPrice |
+-----------+--------------+
|         1 |          7644|
|         2 |         90843|
|         3 |          9083|
|         4 |         10000|
+-----------+--------------+
4 rows in set (0.00 sec)

Following is the query to update MySQL table row column. At first, we have set a user-defined variable, which we will append later −

mysql> set @AppendValue:=500;
Query OK, 0 rows affected (0.00 sec)
mysql> update DemoTable1349 set ProductPrice=ProductPrice+@AppendValue where ProductId=4;
Query OK, 1 row affected (0.13 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable1349;

This will produce the following output −

+-----------+--------------+
| ProductId | ProductPrice |
+-----------+--------------+
|         1 |          7644|
|         2 |         90843|
|         3 |          9083|
|         4 |         10500|
+-----------+--------------+
4 rows in set (0.00 sec)

Updated on: 05-Nov-2019

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements