How do I add to each row in MySQL?

MySQLMySQLi Database

You can add a value to each row in MySQL using UPDATE command.

Let us see when your column is an integer. The syntax is as follows:

UPDATE yourTableName SET yourIntegerColumnName = yourIntegerColumnName+anyValue;
UPDATE yourTableName SET yourIntegerColumnName = anyValue WHERE yourIntegerColumnName IS NULL;

You can add a value for a date column name. The syntax is as follows:

UPDATE yourTableName SET yourDateColumnName = DATEADD(yourDateColumnName,INTERVAL anyIntegerMonth)

To understand the above syntax, let us create a table. The query to create a table is as follows:

mysql> create table addEachRowValue
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Amount int,
   -> ShippingDate date,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.50 sec)

Insert some records in the table using insert command. The query is as follows:

mysql> insert into addEachRowValue(Amount,ShippingDate) values(100,'2019-02-21');
Query OK, 1 row affected (0.15 sec)
mysql> insert into addEachRowValue(Amount,ShippingDate) values(10,'2013-04-22');
Query OK, 1 row affected (0.19 sec)
mysql> insert into addEachRowValue(Amount,ShippingDate) values(110,'2015-10-25');
Query OK, 1 row affected (0.15 sec)
mysql> insert into addEachRowValue(Amount,ShippingDate) values(150,'2016-03-27');
Query OK, 1 row affected (0.29 sec)
mysql> insert into addEachRowValue(Amount,ShippingDate) values(190,'2018-12-29');
Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement. The query is as follows:

mysql> select *from addEachRowValue;

The following is the output:

+----+--------+--------------+
| Id | Amount | ShippingDate |
+----+--------+--------------+
|  1 |    100 | 2019-02-21   |
|  2 |     10 | 2013-04-22   |
|  3 |    110 | 2015-10-25   |
|  4 |    150 | 2016-03-27   |
|  5 |    190 | 2018-12-29   |
+----+--------+--------------+
5 rows in set (0.00 sec)

Here is the query to add a value to each row in Amount column which is a type of integer:

mysql> update addEachRowValue
   -> set Amount=Amount+20;
Query OK, 5 rows affected (0.85 sec)
Rows matched: 5 Changed: 5 Warnings: 0

Now you can check all updated values of Amount column. The query is as follows:

mysql> select Amount from addEachRowValue;

The following is the output:

+--------+
| Amount |
+--------+
|    120 |
|     30 |
|    130 |
|    170 |
|    210 |
+--------+
5 rows in set (0.00 sec)

Here is the query to add a value for a date column:

mysql> update addEachRowValue
   -> set ShippingDate=adddate(ShippingDate,interval 1 month);
Query OK, 5 rows affected (0.31 sec)
Rows matched: 5 Changed: 5 Warnings: 0

Now you can check all updated values of date column from a table. The query is as follows:

mysql> select ShippingDate from addEachRowValue;

The following is the output:

+--------------+
| ShippingDate |
+--------------+
| 2019-03-21   |
| 2013-05-22   |
| 2015-11-25   |
| 2016-04-27   |
| 2019-01-29   |
+--------------+
5 rows in set (0.00 sec)
raja
Published on 25-Feb-2019 09:59:02
Advertisements